-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeat_3.dart
42 lines (33 loc) · 1.05 KB
/
repeat_3.dart
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
static bool mapHasEntry({required Map<String, int> map, required String key}) {
for (var i = 0; i < map.entries.length; i++) {
if (map.keys.toList()[i] == key) return true;
}
return false;
}
static bool isAnagram(String s, String t) {
if (t.length != s.length) return false;
Map<String, int> frequenciesForT = {};
Map<String, int> frequenciesForS = {};
for (var i = 0; i < t.length; i++) {
if (mapHasEntry(map: frequenciesForT, key: t[i])) {
frequenciesForT.update(t[i], (value) => value + 1);
} else {
frequenciesForT.addAll({t[i]: 1});
}
if (mapHasEntry(map: frequenciesForS, key: s[i])) {
frequenciesForS.update(s[i], (value) => value + 1);
} else {
frequenciesForS.addAll({s[i]: 1});
}
}
for (var i = 0; i < t.length; i++) {
if (frequenciesForT[t[i]] != frequenciesForS[t[i]]) return false;
}
return true;
}
}
void main(List<String> args) {
bool result = Solution.isAnagram('rat', 'car');
print(result);
}