-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsomorphicString_205.java
More file actions
74 lines (64 loc) · 2.28 KB
/
Copy pathIsomorphicString_205.java
File metadata and controls
74 lines (64 loc) · 2.28 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.leetcode.string;
import java.util.HashMap;
import java.util.Map;
/**
* Created by charles on 4/9/17.
* Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given "egg", "add", return true.
Given "foo", "bar", return false.
Given "paper", "title", return true.
*/
public class IsomorphicString_205 {
/**
* be careful of sequence of characters
* Thus, should have map <Char, Char>
* find first mapping from char in s to char in t
*/
public boolean isIsomorphic(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] map = new int[128]; // mapping from char in s to char in t
int[] count = new int[128];
for (int i = 0; i < s.length(); i++) {
int sVal = (int) s.charAt(i);
int tVal = (int) t.charAt(i);
if (map[sVal] == 0 && count[tVal] == 0) {
// if conditioned, then this is valid mapping from Char in s to char in t
// else either char in s or t is used
map[sVal] = tVal;
count[tVal] = 1;
} else if (map[sVal] != tVal) {
// if char in t is used
return false;
}
}
return true;
}
public boolean isIsomorphicII(String s, String t) {
Map<Character, Character> map = new HashMap<>();
Map<Character, Character> reverseMap = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char a = s.charAt(i);
char b = t.charAt(i);
if (map.containsKey(a)) {
if (map.get(a).equals(b)) {
continue;
} else {
return false;
}
} else {
if (!reverseMap.containsKey(b)) {
map.put(a,b);
reverseMap.put(b,a);
} else {
return false;
}
}
}
return true;
}
}