Skip to content

Commit 5a6c5c3

Browse files
committed
Create: 0049-group-anagrams.dart
1 parent 4b9a4e3 commit 5a6c5c3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

dart/0049-group-anagrams.dart

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time Complexity: O(n * m)
2+
// Space Complexity: O(n * m)
3+
4+
class Solution {
5+
List<List<String>> groupAnagrams(List<String> strs) {
6+
var map = Map<String, List<String>>();
7+
8+
for (var str in strs) {
9+
var count = List.filled(26, 0);
10+
for (int i = 0; i < str.length; i++) {
11+
count[str[i].codeUnitAt(0) - 'a'.codeUnitAt(0)]++;
12+
}
13+
var key = count.join("#");
14+
if (map.containsKey(key)) {
15+
map[key]!.add(str);
16+
continue;
17+
}
18+
map[key] = [str];
19+
}
20+
21+
return List.from(map.values);
22+
}
23+
}

0 commit comments

Comments
 (0)