-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0242-VALID-ANAGRAM Stats: Time: 20 ms (71.23%), Space: 38 MB (37.85%)…
… - LeetHub
- Loading branch information
1 parent
697b1d2
commit 528161d
Showing
4 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Solution { | ||
fun isAnagram(s: String, t: String): Boolean { | ||
if (s.length != t.length) return false | ||
val anagramMap = mutableMapOf<Char, Int>() | ||
s.forEach { | ||
val count = anagramMap.get(it) | ||
if (count == null) { | ||
anagramMap[it] = 1 | ||
} else { | ||
anagramMap[it] = count + 1 | ||
} | ||
} | ||
|
||
t.forEach { | ||
val count = anagramMap.get(it) | ||
if (count == null) { | ||
anagramMap[it] = 1 | ||
} else { | ||
anagramMap[it] = count - 1 | ||
} | ||
} | ||
anagramMap.forEach { | ||
if (it.value != 0) return false | ||
} | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<h2><a href="https://leetcode.com/problems/valid-anagram">242. Valid Anagram</a></h2><h3>Easy</h3><hr><p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">s = "anagram", t = "nagaram"</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">true</span></p> | ||
</div> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">s = "rat", t = "car"</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">false</span></p> | ||
</div> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= s.length, t.length <= 5 * 10<sup>4</sup></code></li> | ||
<li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> | ||
</ul> | ||
|
||
<p> </p> | ||
<p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"leetcode":{"shas":{"0118-pascals-triangle":{"sha":"05fcd72003bb82a3128c0fc064399628a7dc4775","difficulty":"easy"},"README.md":{"":"548cf6a16b135c24f3e06220e33972b3f15e12aa"},"0136-single-number":{"sha":"c0484dc97f31762bd0acbbdfb43f854ab5f54ebb","difficulty":"easy"},"0268-missing-number":{"sha":"432f05076ad99267d349743103f083d5074e6729","difficulty":"easy"},"0169-majority-element":{"sha":"0ad685da2f57963fdcf125f5ab1fc83cf9ebe64d","difficulty":"easy"},"0217-contains-duplicate":{"sha":"cdcc4153f766598eac814139c8db336378fa2b95","difficulty":"easy"},"0026-remove-duplicates-from-sorted-array":{"sha":"3b81bf6cffcf6df636d4c3cfe400256323845426","difficulty":"easy"},"0066-plus-one":{"sha":"c186ebff3acc9587750c72feaf5f0bf5c7846493","difficulty":"easy"},"0001-two-sum":{"sha":"d3619d155af746e3bba9ebe06ccad94469833e97","difficulty":"easy"},"0412-fizz-buzz":{"sha":"","difficulty":"easy"}},"solved":9,"easy":9,"medium":0,"hard":0}} | ||
{"leetcode":{"shas":{"0118-pascals-triangle":{"sha":"05fcd72003bb82a3128c0fc064399628a7dc4775","difficulty":"easy"},"README.md":{"":"cd3da83b5992887f0e574e45b8a046084a678ac3"},"0136-single-number":{"sha":"c0484dc97f31762bd0acbbdfb43f854ab5f54ebb","difficulty":"easy"},"0268-missing-number":{"sha":"432f05076ad99267d349743103f083d5074e6729","difficulty":"easy"},"0169-majority-element":{"sha":"0ad685da2f57963fdcf125f5ab1fc83cf9ebe64d","difficulty":"easy"},"0217-contains-duplicate":{"sha":"cdcc4153f766598eac814139c8db336378fa2b95","difficulty":"easy"},"0026-remove-duplicates-from-sorted-array":{"sha":"3b81bf6cffcf6df636d4c3cfe400256323845426","difficulty":"easy"},"0066-plus-one":{"sha":"c186ebff3acc9587750c72feaf5f0bf5c7846493","difficulty":"easy"},"0001-two-sum":{"sha":"d3619d155af746e3bba9ebe06ccad94469833e97","difficulty":"easy"},"0412-fizz-buzz":{"sha":"697b1d21bc41e6cbce13f794a99ccf4b5e29f6fc","difficulty":"easy"},"0242-valid-anagram":{"sha":"","difficulty":"easy"}},"solved":10,"easy":10,"medium":0,"hard":0}} |