Skip to content

Commit cf4fe60

Browse files
refactor 205
1 parent 81f78b3 commit cf4fe60

File tree

1 file changed

+29
-25
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+29
-25
lines changed

‎src/main/java/com/fishercoder/solutions/_205.java

+29-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5-
/**Given two strings s and t, determine if they are isomorphic.
5+
/**
6+
* 205. Isomorphic Strings
7+
*
8+
* Given two strings s and t, determine if they are isomorphic.
69
710
Two strings are isomorphic if the characters in s can be replaced to get t.
811
@@ -20,32 +23,33 @@
2023
public class _205 {
2124
/**space should be O(1) since it only has alphabetic letters which are capped at 52.*/
2225

23-
public boolean isIsomorphic(String s, String t) {
24-
if (s == null || s.length() == 0) {
25-
return (t == null || t.length() == 0);
26-
}
27-
if (t == null || t.length() == 0) {
28-
return (s == null || s.length() == 0);
29-
}
30-
char[] schar = s.toCharArray();
31-
char[] tchar = t.toCharArray();
32-
Map<Character, Character> map = new HashMap();
33-
if (s.length() != t.length()) {
34-
return false;
35-
}
36-
for (int i = 0; i < s.length(); i++) {
37-
if (map.containsKey(schar[i])) {
38-
if (map.get(schar[i]) != tchar[i]) {
39-
return false;
40-
}
41-
} else {
42-
if (map.containsValue(tchar[i])) {
43-
return false;//this line is necessary for this case: ("ab", "aa")
26+
public static class Solution1 {
27+
public boolean isIsomorphic(String s, String t) {
28+
if (s == null || s.length() == 0) {
29+
return (t == null || t.length() == 0);
30+
}
31+
if (t == null || t.length() == 0) {
32+
return (s == null || s.length() == 0);
33+
}
34+
char[] schar = s.toCharArray();
35+
char[] tchar = t.toCharArray();
36+
Map<Character, Character> map = new HashMap();
37+
if (s.length() != t.length()) {
38+
return false;
39+
}
40+
for (int i = 0; i < s.length(); i++) {
41+
if (map.containsKey(schar[i])) {
42+
if (map.get(schar[i]) != tchar[i]) {
43+
return false;
44+
}
45+
} else {
46+
if (map.containsValue(tchar[i])) {
47+
return false;//this line is necessary for this case: ("ab", "aa")
48+
}
49+
map.put(schar[i], tchar[i]);
4450
}
45-
map.put(schar[i], tchar[i]);
4651
}
52+
return true;
4753
}
48-
return true;
4954
}
50-
5155
}

0 commit comments

Comments
 (0)