-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIsomorphicStrings.java
39 lines (33 loc) · 1.02 KB
/
IsomorphicStrings.java
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
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.HashSet;
import java.util.Set;
// https://leetcode.com/problems/isomorphic-strings/
public class IsomorphicStrings {
private final String s;
private final String t;
public IsomorphicStrings(String s, String t) {
this.s = s;
this.t = t;
}
public boolean solution() {
char[] replaces = new char[128];
Set<Character> replaceValues = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
char currS = s.charAt(i);
char currT = t.charAt(i);
if (replaces[currS] != 0) {
char replacement = replaces[currS];
if (currT != replacement) {
return false;
}
} else {
if (replaceValues.contains(currT)) {
return false;
}
replaces[currS] = currT;
replaceValues.add(currT);
}
}
return true;
}
}