-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsomorphicStrings.java
More file actions
34 lines (26 loc) · 954 Bytes
/
Copy pathIsomorphicStrings.java
File metadata and controls
34 lines (26 loc) · 954 Bytes
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
package LeetcodePractice;
import java.util.*;
public class IsomorphicStrings {
public static boolean isIsomorphic(String s, String t) {
if (s.length() != t.length()) return false;
Map<Character, Character> mapST = new HashMap<>();
Set<Character> mapped = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
char chS = s.charAt(i);
char chT = t.charAt(i);
if (mapST.containsKey(chS)) {
if (mapST.get(chS) != chT) return false;
} else {
if (mapped.contains(chT)) return false;
mapST.put(chS, chT);
mapped.add(chT);
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isIsomorphic("egg", "add"));
System.out.println(isIsomorphic("foo", "bar"));
System.out.println(isIsomorphic("paper", "title"));
}
}