|
2 | 2 |
|
3 | 3 | import java.util.HashMap;
|
4 | 4 | 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. |
6 | 9 |
|
7 | 10 | Two strings are isomorphic if the characters in s can be replaced to get t.
|
8 | 11 |
|
|
20 | 23 | public class _205 {
|
21 | 24 | /**space should be O(1) since it only has alphabetic letters which are capped at 52.*/
|
22 | 25 |
|
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]); |
44 | 50 | }
|
45 |
| - map.put(schar[i], tchar[i]); |
46 | 51 | }
|
| 52 | + return true; |
47 | 53 | }
|
48 |
| - return true; |
49 | 54 | }
|
50 |
| - |
51 | 55 | }
|
0 commit comments