1
+ // Runtime: 698 ms (Top 5.47%) | Memory: 145 MB (Top 5.47%)
2
+
1
3
class Solution {
2
4
public int numSpecialEquivGroups (String [] words ) {
3
5
if (words .length == 0 || words .length == 1 ) return words .length ;
4
-
5
- // To store group sizes
6
+
7
+ // To store group sizes
6
8
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
9
11
boolean [] isGrouped = new boolean [words .length ];
10
-
12
+
11
13
for (int index = 0 ; index < words .length ; index ++) {
12
14
if (isGrouped [index ]) continue ; // Already grouped
13
15
String word = words [index ];
14
16
for (int j = index + 1 ; j < words .length ; j ++) {
15
- if (isGrouped [j ]) continue ; // Already grouped
17
+ if (isGrouped [j ]) continue ; // Already grouped
16
18
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
22
24
HashMap <Character , Integer > evens = new HashMap <>();
23
25
HashMap <Character , Integer > odds = new HashMap <>();
24
26
boolean isSpecialEquivalent = true ;
25
-
27
+
26
28
for (int i = 0 ; i < word .length (); i ++) {
27
29
if (i % 2 == 0 ) {
28
30
evens .put (word .charAt (i ), evens .getOrDefault (word .charAt (i ), 0 ) + 1 );
29
31
} else {
30
32
odds .put (word .charAt (i ), odds .getOrDefault (word .charAt (i ), 0 ) + 1 );
31
33
}
32
34
}
33
-
35
+
34
36
for (int i = 0 ; i < string .length (); i ++) {
35
37
char character = string .charAt (i );
36
38
if (i % 2 == 0 ) {
37
39
if (!evens .containsKey (character )) {
38
40
isSpecialEquivalent = false ;
39
41
break ;
40
42
}
41
-
43
+
42
44
evens .put (character , evens .get (character ) - 1 );
43
45
if (evens .get (character ) == 0 ) evens .remove (character );
44
46
} else {
45
47
if (!odds .containsKey (character )) {
46
48
isSpecialEquivalent = false ;
47
49
break ;
48
50
}
49
-
51
+
50
52
odds .put (character , odds .get (character ) - 1 );
51
53
if (odds .get (character ) == 0 ) odds .remove (character );
52
54
}
53
55
}
54
-
56
+
55
57
if (isSpecialEquivalent ) {
56
58
hashmap .put (word , hashmap .getOrDefault (word , 0 ) + 1 );
57
59
isGrouped [j ] = true ;
58
60
}
59
61
}
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
62
64
if (!hashmap .containsKey (word )) hashmap .put (word , 1 );
63
65
}
64
-
66
+
65
67
return hashmap .size ();
66
68
}
67
- }
69
+ }
0 commit comments