File tree 4 files changed +97
-14
lines changed
solution/0200-0299/0242.Valid Anagram
4 files changed +97
-14
lines changed Original file line number Diff line number Diff line change 28
28
29
29
<!-- 这里可写通用的实现逻辑 -->
30
30
31
+ 哈希表解决。
32
+
31
33
<!-- tabs:start -->
32
34
33
35
### ** Python3**
34
36
35
37
<!-- 这里可写当前语言的特殊实现逻辑 -->
36
38
37
39
``` python
38
-
40
+ class Solution :
41
+ def isAnagram (self , s : str , t : str ) -> bool :
42
+ if len (s) != len (t):
43
+ return False
44
+ n = len (s)
45
+ chars = [0 ] * 26
46
+ for i in range (n):
47
+ chars[ord (s[i]) - ord (' a' )] += 1
48
+ chars[ord (t[i]) - ord (' a' )] -= 1
49
+ for i in range (26 ):
50
+ if chars[i] != 0 :
51
+ return False
52
+ return True
39
53
```
40
54
41
55
### ** Java**
42
56
43
57
<!-- 这里可写当前语言的特殊实现逻辑 -->
44
58
45
59
``` java
46
-
60
+ class Solution {
61
+ public boolean isAnagram (String s , String t ) {
62
+ int n;
63
+ if ((n = s. length()) != t. length()) {
64
+ return false ;
65
+ }
66
+ int [] chars = new int [26 ];
67
+ for (int i = 0 ; i < n; ++ i) {
68
+ ++ chars[s. charAt(i) - ' a' ];
69
+ -- chars[t. charAt(i) - ' a' ];
70
+ }
71
+ for (int i = 0 ; i < 26 ; ++ i) {
72
+ if (chars[i] != 0 ) {
73
+ return false ;
74
+ }
75
+ }
76
+ return true ;
77
+ }
78
+ }
47
79
```
48
80
49
81
### ** ...**
Original file line number Diff line number Diff line change @@ -41,13 +41,43 @@ What if the inputs contain unicode characters? How would you adapt your solution
41
41
### ** Python3**
42
42
43
43
``` python
44
-
44
+ class Solution :
45
+ def isAnagram (self , s : str , t : str ) -> bool :
46
+ if len (s) != len (t):
47
+ return False
48
+ n = len (s)
49
+ chars = [0 ] * 26
50
+ for i in range (n):
51
+ chars[ord (s[i]) - ord (' a' )] += 1
52
+ chars[ord (t[i]) - ord (' a' )] -= 1
53
+ for i in range (26 ):
54
+ if chars[i] != 0 :
55
+ return False
56
+ return True
45
57
```
46
58
47
59
### ** Java**
48
60
49
61
``` java
50
-
62
+ class Solution {
63
+ public boolean isAnagram (String s , String t ) {
64
+ int n;
65
+ if ((n = s. length()) != t. length()) {
66
+ return false ;
67
+ }
68
+ int [] chars = new int [26 ];
69
+ for (int i = 0 ; i < n; ++ i) {
70
+ ++ chars[s. charAt(i) - ' a' ];
71
+ -- chars[t. charAt(i) - ' a' ];
72
+ }
73
+ for (int i = 0 ; i < 26 ; ++ i) {
74
+ if (chars[i] != 0 ) {
75
+ return false ;
76
+ }
77
+ }
78
+ return true ;
79
+ }
80
+ }
51
81
```
52
82
53
83
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public boolean isAnagram (String s , String t ) {
3
- char [] val1 = s .toCharArray ();
4
- char [] val2 = t .toCharArray ();
5
- Arrays .sort (val1 );
6
- Arrays .sort (val2 );
7
- String s1 = new String (val1 );
8
- String s2 = new String (val2 );
9
- return s1 .equals (s2 );
10
- }
11
- }
2
+ public boolean isAnagram (String s , String t ) {
3
+ int n ;
4
+ if ((n = s .length ()) != t .length ()) {
5
+ return false ;
6
+ }
7
+ int [] chars = new int [26 ];
8
+ for (int i = 0 ; i < n ; ++i ) {
9
+ ++chars [s .charAt (i ) - 'a' ];
10
+ --chars [t .charAt (i ) - 'a' ];
11
+ }
12
+ for (int i = 0 ; i < 26 ; ++i ) {
13
+ if (chars [i ] != 0 ) {
14
+ return false ;
15
+ }
16
+ }
17
+ return true ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isAnagram (self , s : str , t : str ) -> bool :
3
+ if len (s ) != len (t ):
4
+ return False
5
+ n = len (s )
6
+ chars = [0 ] * 26
7
+ for i in range (n ):
8
+ chars [ord (s [i ]) - ord ('a' )] += 1
9
+ chars [ord (t [i ]) - ord ('a' )] -= 1
10
+ for i in range (26 ):
11
+ if chars [i ] != 0 :
12
+ return False
13
+ return True
You can’t perform that action at this time.
0 commit comments