File tree 4 files changed +72
-37
lines changed
solution/0200-0299/0205.Isomorphic Strings
4 files changed +72
-37
lines changed Original file line number Diff line number Diff line change 41
41
<!-- 这里可写当前语言的特殊实现逻辑 -->
42
42
43
43
``` 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
45
55
```
46
56
47
57
### ** Java**
48
58
49
59
<!-- 这里可写当前语言的特殊实现逻辑 -->
50
60
51
61
``` 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
+ }
53
76
```
54
77
55
78
### ** ...**
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
47
47
### ** Python3**
48
48
49
49
``` 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
51
61
```
52
62
53
63
### ** Java**
54
64
55
65
``` 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
+ }
57
80
```
58
81
59
82
### ** ...**
Original file line number Diff line number Diff line change 1
- public class Solution {
1
+ class Solution {
2
2
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 ;
35
13
}
36
14
}
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