-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0242_valid_anagram.kt
28 lines (22 loc) · 961 Bytes
/
0242_valid_anagram.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// https://leetcode.com/problems/valid-anagram/
// O(nLogn) solution, bruteforce
class Solution {
fun isAnagram(s: String, t: String): Boolean {
if (s.length != t.length) return false;
return s.toCharArray().sorted().equals(t.toCharArray().sorted())
}
}
// O(n+m) solution, where n is the length of s & m is the length of t
class Solution {
fun isAnagram(s: String, t: String): Boolean {
if (s.length != t.length) return false;
val sHashMap : HashMap<Char, Int> = HashMap<Char, Int>()
val tHashMap : HashMap<Char, Int> = HashMap<Char, Int>()
// Can assume same length, only 1 loop is necessary.
for (i in s.indices) {
sHashMap[s[i]] = sHashMap.getOrDefault(s[i], 0) + 1
tHashMap[t[i]] = tHashMap.getOrDefault(t[i], 0) + 1
}
return sHashMap == tHashMap // Apparently this checks for structural equality only, not referential. So it works.
}
}