Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit48f1bd0

Browse files
isomorphic strings
1 parent9febd2e commit48f1bd0

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+
packageeasy;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
6+
publicclassIsomorphicStrings {
7+
/**space should be O(1) since it only has alphabetic letters which are capped at 52.*/
8+
9+
publicbooleanisIsomorphic(Strings,Stringt) {
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 =newHashMap();
15+
if(s.length() !=t.length())returnfalse;
16+
for(inti =0;i <s.length();i++){
17+
if(map.containsKey(schar[i])){
18+
if(map.get(schar[i]) !=tchar[i])returnfalse;
19+
}else {
20+
if(map.containsValue(tchar[i]))returnfalse;//this line is necessary for this case: ("ab", "aa")
21+
map.put(schar[i],tchar[i]);
22+
}
23+
}
24+
returntrue;
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)

[8]ページ先頭

©2009-2025 Movatter.jp