Skip to content

Commit 65cac1e

Browse files
committed
Create: 0242-valid-anagram.dart
1 parent 9a3440c commit 65cac1e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: dart/0242-valid-anagram.dart

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time Complexity: O(s + t)
2+
// Space Complexity: O(1)
3+
4+
class Solution {
5+
bool isAnagram(String s, String t) {
6+
if (s.length != t.length) return false;
7+
8+
final counter = List.filled(26, 0);
9+
10+
for (int i = 0; i < s.length; i++) {
11+
counter[s[i].codeUnitAt(0) - 'a'.codeUnitAt(0)]++;
12+
}
13+
14+
for (int i = 0; i < t.length; i++) {
15+
counter[t[i].codeUnitAt(0) - 'a'.codeUnitAt(0)]--;
16+
}
17+
18+
for (int val in counter) {
19+
if (val != 0) return false;
20+
}
21+
22+
return true;
23+
}
24+
}

0 commit comments

Comments
 (0)