Skip to content

Commit 48f1bd0

Browse files
isomorphic strings
1 parent 9febd2e commit 48f1bd0

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

EASY/src/easy/IsomorphicStrings.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package easy;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class IsomorphicStrings {
7+
/**space should be O(1) since it only has alphabetic letters which are capped at 52.*/
8+
9+
public boolean isIsomorphic(String s, String t) {
10+
if(s == null || s.length() == 0) return (t == null || t.length() == 0);
11+
if(t == null || t.length() == 0) return (s == null || s.length() == 0);
12+
char[] schar = s.toCharArray();
13+
char[] tchar = t.toCharArray();
14+
Map<Character, Character> map = new HashMap();
15+
if(s.length() != t.length()) return false;
16+
for(int i = 0; i < s.length(); i++){
17+
if(map.containsKey(schar[i])){
18+
if(map.get(schar[i]) != tchar[i]) return false;
19+
} else {
20+
if(map.containsValue(tchar[i])) return false;//this line is necessary for this case: ("ab", "aa")
21+
map.put(schar[i], tchar[i]);
22+
}
23+
}
24+
return true;
25+
}
26+
27+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)|[Solution](../../blob/master/EASY/src/easy/BinaryTreePaths.java) | O(n*h) | O(h) | DFS/Recursion
2727
|209|[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)|[Solution](../../blob/master/MEDIUM/src/medium/MinimumSizeSubarraySum.java)| O(n)|O(1) | Medium|
2828
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)|[Solution](../../blob/master/EASY/src/easy/ReverseLinkedList.java)| O(n)|O(1) | Easy
29+
|205|[Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/)|[Solution](../../blob/master/EASY/src/easy/IsomorphicStrings.java)| O(n)|O(1) | Easy
2930
|200|[Number of Islands](https://leetcode.com/problems/number-of-islands/)|[Union Find](../../blob/master/MEDIUM/src/medium/NumberOfIslandsUnionFind.java) [DFS](../../blob/master/MEDIUM/src/medium/NumberofIslandsDFS.java)| O(m*n)|O(m*n) | Medium| Union Find, DFS
3031
|189|[Rotate Array](https://leetcode.com/problems/rotate-array/)|[Solution](../../blob/master/EASY/src/easy/RotateArray.java)| O(n)|O(n), could be optimized to O(1) | Easy
3132
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)|[Queue](../../blob/master/MEDIUM/src/medium/BSTIterator_using_q.java) [Stack](../../blob/master/MEDIUM/src/medium/BSTIterator_using_stack.java)| O(1) |O(h) | Medium|

0 commit comments

Comments
 (0)