-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIsomorphicWords
43 lines (36 loc) · 1.12 KB
/
IsomorphicWords
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.*;
public class IsomorphicWords {
public int countPairs(String[] words) {
int count = 0;
for (int i = 0; i < words.length; i++){
for (int j = i + 1; j < words.length; j++){
if (i != j){
HashMap<Character, Character> mapa = new HashMap<Character, Character>();
boolean x = true;
for (int k = 0; k < words[i].length(); k++){
if (!mapa.containsKey(words[i].charAt(k))){
mapa.put(words[i].charAt(k), words[j].charAt(k));
}else{
if(mapa.get(words[i].charAt(k)) != words[j].charAt(k)){
x = false;
break;
}
}
}
if (x){
count++;
}
}
}
}
return count;
}
public static void main(String [] args){
IsomorphicWords tester = new IsomorphicWords();
String [] names = {"aa", "ab", "bb", "cc", "cd"};
tester.countPairs(names);
System.out.print(tester.countPairs(names));
}
}