File tree Expand file tree Collapse file tree 4 files changed +72
-37
lines changed
solution/0200-0299/0205.Isomorphic Strings Expand file tree Collapse file tree 4 files changed +72
-37
lines changed Original file line number Diff line number Diff line change 4141<!-- 这里可写当前语言的特殊实现逻辑 -->
4242
4343``` python
44-
44+ class Solution :
45+ def isIsomorphic (self , s : str , t : str ) -> bool :
46+ a2b, b2a = {}, {}
47+ n = len (s)
48+ for i in range (n):
49+ a, b = s[i], t[i]
50+ if (a in a2b and a2b[a] != b) or (b in b2a and b2a[b] != a):
51+ return False
52+ a2b[a] = b
53+ b2a[b] = a
54+ return True
4555```
4656
4757### ** Java**
4858
4959<!-- 这里可写当前语言的特殊实现逻辑 -->
5060
5161``` java
52-
62+ class Solution {
63+ public boolean isIsomorphic (String s , String t ) {
64+ int n = s. length();
65+ Map<Character , Character > a2b = new HashMap<> ();
66+ Map<Character , Character > b2a = new HashMap<> ();
67+ for (int i = 0 ; i < n; ++ i) {
68+ char a = s. charAt(i), b = t. charAt(i);
69+ if ((a2b. containsKey(a) && a2b. get(a) != b) || (b2a. containsKey(b) && b2a. get(b) != a)) return false ;
70+ a2b. put(a, b);
71+ b2a. put(b, a);
72+ }
73+ return true ;
74+ }
75+ }
5376```
5477
5578### ** ...**
Original file line number Diff line number Diff line change @@ -47,13 +47,36 @@ You may assume both <b><i>s </i></b>and <b><i>t </i></b>have the same
4747### ** Python3**
4848
4949``` python
50-
50+ class Solution :
51+ def isIsomorphic (self , s : str , t : str ) -> bool :
52+ a2b, b2a = {}, {}
53+ n = len (s)
54+ for i in range (n):
55+ a, b = s[i], t[i]
56+ if (a in a2b and a2b[a] != b) or (b in b2a and b2a[b] != a):
57+ return False
58+ a2b[a] = b
59+ b2a[b] = a
60+ return True
5161```
5262
5363### ** Java**
5464
5565``` java
56-
66+ class Solution {
67+ public boolean isIsomorphic (String s , String t ) {
68+ int n = s. length();
69+ Map<Character , Character > a2b = new HashMap<> ();
70+ Map<Character , Character > b2a = new HashMap<> ();
71+ for (int i = 0 ; i < n; ++ i) {
72+ char a = s. charAt(i), b = t. charAt(i);
73+ if ((a2b. containsKey(a) && a2b. get(a) != b) || (b2a. containsKey(b) && b2a. get(b) != a)) return false ;
74+ a2b. put(a, b);
75+ b2a. put(b, a);
76+ }
77+ return true ;
78+ }
79+ }
5780```
5881
5982### ** ...**
Original file line number Diff line number Diff line change 1- public class Solution {
1+ class Solution {
22 public boolean isIsomorphic (String s , String t ) {
3-
4- if (s == null || t == null ) {
5- return false ;
6- }
7-
8- if (s .length () != t .length ()) {
9- return false ;
10- }
11-
12- Map <Character , Character > map = new HashMap <Character , Character >();
13- Set <Character > set = new HashSet <Character >();
14-
15- for (int i = 0 ; i < s .length (); i ++) {
16- char c1 = s .charAt (i );
17- char c2 = t .charAt (i );
18-
19- if (map .containsKey (c1 )) {
20- if (map .get (c1 ) != c2 ) {
21- return false ;
22- }
23- } else {
24- if (set .contains (c2 )) {
25- return false ;
26- }
27- map .put (c1 , c2 );
28- set .add (c2 );
29- }
30- }
31-
32- return true ;
33-
34-
3+ int n = s .length ();
4+ Map <Character , Character > a2b = new HashMap <>();
5+ Map <Character , Character > b2a = new HashMap <>();
6+ for (int i = 0 ; i < n ; ++i ) {
7+ char a = s .charAt (i ), b = t .charAt (i );
8+ if ((a2b .containsKey (a ) && a2b .get (a ) != b ) || (b2a .containsKey (b ) && b2a .get (b ) != a )) return false ;
9+ a2b .put (a , b );
10+ b2a .put (b , a );
11+ }
12+ return true ;
3513 }
3614}
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def isIsomorphic (self , s : str , t : str ) -> bool :
3+ a2b , b2a = {}, {}
4+ n = len (s )
5+ for i in range (n ):
6+ a , b = s [i ], t [i ]
7+ if (a in a2b and a2b [a ] != b ) or (b in b2a and b2a [b ] != a ):
8+ return False
9+ a2b [a ] = b
10+ b2a [b ] = a
11+ return True
You can’t perform that action at this time.
0 commit comments