-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram126.java
More file actions
50 lines (40 loc) · 1.5 KB
/
Program126.java
File metadata and controls
50 lines (40 loc) · 1.5 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
import java.util.*;
public class Program126 {
// Isomorphic Strings :
// Check if strings s and t are isomorphic:
// Every character in s maps to exactly one character in t, and vice versa.
public static boolean isIsomorphic(String s, String t) {
if (s.length() != t.length()) {
return false;
}
Map<Character, Character> mapST = new HashMap<>(); // s → t
Map<Character, Character> mapTS = new HashMap<>(); // t → s
for (int i = 0; i < s.length(); i++) {
char c1 = s.charAt(i); // char from s
char c2 = t.charAt(i); // char from t
// Check mapping from s → t
if (mapST.containsKey(c1)) {
if (mapST.get(c1) != c2) {
return false;
}
} else {
mapST.put(c1, c2);
}
// Check reverse mapping from t → s
if (mapTS.containsKey(c2)) {
if (mapTS.get(c2) != c1) {
return false;
}
} else {
mapTS.put(c2, c1);
}
}
return true;
}
public static void main(String[] args) {
String s1 = "egg", t1 = "add"; // true
String s2 = "foo", t2 = "bar"; // false
System.out.println("Is egg and add isomorphic? " + isIsomorphic(s1, t1));
System.out.println("Is foo and bar isomorphic? " + isIsomorphic(s2, t2));
}
}