From 528161d1b3edcfc2aaf225bce6ecedf908edb3fd Mon Sep 17 00:00:00 2001 From: Sudhir Singh Khanger Date: Mon, 18 Nov 2024 15:50:53 +0530 Subject: [PATCH] 0242-VALID-ANAGRAM Stats: Time: 20 ms (71.23%), Space: 38 MB (37.85%) - LeetHub --- 0242-valid-anagram/0242-valid-anagram.kt | 27 ++++++++++++++++++++++ 0242-valid-anagram/README.md | 29 ++++++++++++++++++++++++ README.md | 3 +++ stats.json | 2 +- 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 0242-valid-anagram/0242-valid-anagram.kt create mode 100644 0242-valid-anagram/README.md diff --git a/0242-valid-anagram/0242-valid-anagram.kt b/0242-valid-anagram/0242-valid-anagram.kt new file mode 100644 index 0000000..22c231c --- /dev/null +++ b/0242-valid-anagram/0242-valid-anagram.kt @@ -0,0 +1,27 @@ +class Solution { + fun isAnagram(s: String, t: String): Boolean { + if (s.length != t.length) return false + val anagramMap = mutableMapOf() + 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 + } +} \ No newline at end of file diff --git a/0242-valid-anagram/README.md b/0242-valid-anagram/README.md new file mode 100644 index 0000000..7216cfc --- /dev/null +++ b/0242-valid-anagram/README.md @@ -0,0 +1,29 @@ +

242. Valid Anagram

Easy


Given two strings s and t, return true if t is an anagram of s, and false otherwise.

+ +

 

+

Example 1:

+ +
+

Input: s = "anagram", t = "nagaram"

+ +

Output: true

+
+ +

Example 2:

+ +
+

Input: s = "rat", t = "car"

+ +

Output: false

+
+ +

 

+

Constraints:

+ + + +

 

+

Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?

diff --git a/README.md b/README.md index cd3da83..4e3c7cf 100644 --- a/README.md +++ b/README.md @@ -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 | | @@ -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 | | @@ -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 | | diff --git a/stats.json b/stats.json index 4062b9a..cb6cdf2 100644 --- a/stats.json +++ b/stats.json @@ -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}} \ No newline at end of file +{"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}} \ No newline at end of file