Skip to content

Commit

Permalink
0242-VALID-ANAGRAM Stats: Time: 20 ms (71.23%), Space: 38 MB (37.85%)…
Browse files Browse the repository at this point in the history
… - LeetHub
  • Loading branch information
sudhirkhanger committed Nov 18, 2024
1 parent 697b1d2 commit 528161d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
27 changes: 27 additions & 0 deletions 0242-valid-anagram/0242-valid-anagram.kt
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
}
}
29 changes: 29 additions & 0 deletions 0242-valid-anagram/README.md
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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;anagram&quot;, t = &quot;nagaram&quot;</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 = &quot;rat&quot;, t = &quot;car&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">false</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>s</code> and <code>t</code> consist of lowercase English letters.</li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> What if the inputs contain Unicode characters? How would you adapt your solution to such a case?</p>
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ A collection of LeetCode questions to ace the coding interviews! - Created using
| [0001-two-sum](https://github.com/sudhirkhanger/Algorithms/tree/master/0001-two-sum) |
| [0169-majority-element](https://github.com/sudhirkhanger/Algorithms/tree/master/0169-majority-element) |
| [0217-contains-duplicate](https://github.com/sudhirkhanger/Algorithms/tree/master/0217-contains-duplicate) |
| [0242-valid-anagram](https://github.com/sudhirkhanger/Algorithms/tree/master/0242-valid-anagram) |
| [0268-missing-number](https://github.com/sudhirkhanger/Algorithms/tree/master/0268-missing-number) |
## Math
| |
Expand All @@ -43,6 +44,7 @@ A collection of LeetCode questions to ace the coding interviews! - Created using
| ------- |
| [0169-majority-element](https://github.com/sudhirkhanger/Algorithms/tree/master/0169-majority-element) |
| [0217-contains-duplicate](https://github.com/sudhirkhanger/Algorithms/tree/master/0217-contains-duplicate) |
| [0242-valid-anagram](https://github.com/sudhirkhanger/Algorithms/tree/master/0242-valid-anagram) |
| [0268-missing-number](https://github.com/sudhirkhanger/Algorithms/tree/master/0268-missing-number) |
## Divide and Conquer
| |
Expand All @@ -59,6 +61,7 @@ A collection of LeetCode questions to ace the coding interviews! - Created using
## String
| |
| ------- |
| [0242-valid-anagram](https://github.com/sudhirkhanger/Algorithms/tree/master/0242-valid-anagram) |
| [0412-fizz-buzz](https://github.com/sudhirkhanger/Algorithms/tree/master/0412-fizz-buzz) |
## Simulation
| |
Expand Down
2 changes: 1 addition & 1 deletion stats.json
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}}

0 comments on commit 528161d

Please sign in to comment.