Skip to content

Commit d36cc6f

Browse files
authored
Create Count Pairs Of Similar Strings.java
1 parent 9c5f812 commit d36cc6f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int similarPairs(String[] words) {
3+
Map<String, Integer> map = new HashMap<>();
4+
int pairCount = 0;
5+
for (String word : words) {
6+
String key = buildSortedCharString(word);
7+
pairCount += map.getOrDefault(key, 0);
8+
map.put(key, map.getOrDefault(key, 0) + 1);
9+
}
10+
return pairCount;
11+
}
12+
13+
private static String buildSortedCharString(String s) {
14+
boolean[] exists = new boolean[26];
15+
for (char c : s.toCharArray()) {
16+
exists[c - 'a'] = true;
17+
}
18+
StringBuilder sb = new StringBuilder();
19+
for (int i = 0; i < 26; i++) {
20+
if (exists[i]) {
21+
sb.append((char) (97 + i));
22+
}
23+
}
24+
return sb.toString();
25+
}
26+
}

0 commit comments

Comments
 (0)