-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm205.java
More file actions
56 lines (50 loc) · 1.74 KB
/
prblm205.java
File metadata and controls
56 lines (50 loc) · 1.74 KB
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
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
public class prblm205 {
public static void main(String[] args) {
String s = "egg";
String t = "add";
System.out.println(isIsomorphic(s, t));
System.out.println(isIsomorphic2(s, t));
String s2 = "foo";
String t2 = "bar";
System.out.println(isIsomorphic(s2, t2));
System.out.println(isIsomorphic2(s2, t2));
String s3 = "paper";
String t3 = "title";
System.out.println(isIsomorphic(s3, t3));
System.out.println(isIsomorphic2(s3, t3));
String s4 = "badc";
String t4 = "baba";
System.out.println(isIsomorphic(s4, t4));
System.out.println(isIsomorphic2(s4, t4));
}
public static boolean isIsomorphic(String s, String t) {
Map<Character, Character> map = new HashMap<>();
Map<Character, Character> map2 = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
if (map.containsKey(s.charAt(i)) && map.get(s.charAt(i)) != t.charAt(i)) {
return false;
}
if (map2.containsKey(t.charAt(i)) && map2.get(t.charAt(i)) != s.charAt(i)) {
return false;
}
map.put(s.charAt(i), t.charAt(i));
map2.put(t.charAt(i), s.charAt(i));
}
return true;
}
public static boolean isIsomorphic2(String s, String t) {
int[] arr1 = new int[256];
int[] arr2 = new int[256];
for (int i = 0; i < s.length(); i++) {
char a = s.charAt(i);
char b = t.charAt(i);
if (arr1[a] != arr2[b]) {
return false;
}
arr1[a] = i + 1;
arr2[b] = i + 1;
}
return true;
}
}