-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCountPairsOfSimilarStrings.java
45 lines (38 loc) · 1.26 KB
/
CountPairsOfSimilarStrings.java
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
43
44
45
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
// https://leetcode.com/problems/count-pairs-of-similar-strings/
public class CountPairsOfSimilarStrings {
private final String[] input;
public CountPairsOfSimilarStrings(String[] input) {
this.input = input;
}
public int solution() {
Map<String, String> map = new HashMap<>();
int result = 0;
for (int i = 0; i < input.length; i++) {
String curr = input[i];
checkExists(curr, map);
String currSet = map.get(curr);
for (int j = i + 1; j < input.length; j++) {
String compare = input[j];
checkExists(compare, map);
if (currSet.compareTo(map.get(compare)) == 0) {
result++;
}
}
}
return result;
}
private void checkExists(String word, Map<String, String> map) {
if (!map.containsKey(word)) {
Set<Character> set = new TreeSet<>();
for (int i = 0; i < word.length(); i++) {
set.add(word.charAt(i));
}
map.put(word, String.valueOf(set));
}
}
}