Skip to content

Commit 5fa356a

Browse files
committed
Runtime: 698 ms (Top 5.47%) | Memory: 145 MB (Top 5.47%)
1 parent 26e8b02 commit 5fa356a

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,69 @@
1+
// Runtime: 698 ms (Top 5.47%) | Memory: 145 MB (Top 5.47%)
2+
13
class Solution {
24
public int numSpecialEquivGroups(String[] words) {
35
if(words.length == 0 || words.length == 1) return words.length;
4-
5-
// To store group sizes
6+
7+
// To store group sizes
68
HashMap<String, Integer> hashmap = new HashMap<>();
7-
8-
// To mark the strings already part of some groups
9+
10+
// To mark the strings already part of some groups
911
boolean[] isGrouped = new boolean[words.length];
10-
12+
1113
for(int index = 0; index < words.length; index++) {
1214
if(isGrouped[index]) continue; // Already grouped
1315
String word = words[index];
1416
for(int j = index + 1; j < words.length; j++) {
15-
if(isGrouped[j]) continue; // Already grouped
17+
if(isGrouped[j]) continue; // Already grouped
1618
String string = words[j];
17-
18-
// The idea is to store count of characters on even and odd indices
19-
// It is done by incrementing counts of characters in both even and odd maps respectively
20-
// Then compare the two strings by reducing the same count in both even and odd maps
21-
// If both the maps are empty at last, the two strings for a group
19+
20+
// The idea is to store count of characters on even and odd indices
21+
// It is done by incrementing counts of characters in both even and odd maps respectively
22+
// Then compare the two strings by reducing the same count in both even and odd maps
23+
// If both the maps are empty at last, the two strings for a group
2224
HashMap<Character, Integer> evens = new HashMap<>();
2325
HashMap<Character, Integer> odds = new HashMap<>();
2426
boolean isSpecialEquivalent = true;
25-
27+
2628
for(int i = 0; i < word.length(); i++) {
2729
if(i % 2 == 0) {
2830
evens.put(word.charAt(i), evens.getOrDefault(word.charAt(i), 0) + 1);
2931
} else {
3032
odds.put(word.charAt(i), odds.getOrDefault(word.charAt(i), 0) + 1);
3133
}
3234
}
33-
35+
3436
for(int i = 0; i < string.length(); i++) {
3537
char character = string.charAt(i);
3638
if(i % 2 == 0) {
3739
if(!evens.containsKey(character)) {
3840
isSpecialEquivalent = false;
3941
break;
4042
}
41-
43+
4244
evens.put(character, evens.get(character) - 1);
4345
if(evens.get(character) == 0) evens.remove(character);
4446
} else {
4547
if(!odds.containsKey(character)) {
4648
isSpecialEquivalent = false;
4749
break;
4850
}
49-
51+
5052
odds.put(character, odds.get(character) - 1);
5153
if(odds.get(character) == 0) odds.remove(character);
5254
}
5355
}
54-
56+
5557
if(isSpecialEquivalent) {
5658
hashmap.put(word, hashmap.getOrDefault(word, 0) + 1);
5759
isGrouped[j] = true;
5860
}
5961
}
60-
61-
// If no group is formed, the word alone forms a group of size 1
62+
63+
// If no group is formed, the word alone forms a group of size 1
6264
if(!hashmap.containsKey(word)) hashmap.put(word, 1);
6365
}
64-
66+
6567
return hashmap.size();
6668
}
67-
}
69+
}

0 commit comments

Comments
 (0)