File tree Expand file tree Collapse file tree 11 files changed +182
-39
lines changed
lcof2/剑指 Offer II 032. 有效的变位词
solution/0200-0299/0242.Valid Anagram Expand file tree Collapse file tree 11 files changed +182
-39
lines changed Original file line number Diff line number Diff line change 46
46
47
47
<p ><meta charset =" UTF-8 " />注意:本题与主站 242  ; 题相似(字母异位词定义不同):<a href =" https://leetcode-cn.com/problems/valid-anagram/ " >https://leetcode-cn.com/problems/valid-anagram/</a ></p >
48
48
49
-
50
49
## 解法
51
50
52
51
<!-- 这里可写通用的实现逻辑 -->
53
52
53
+ 数组或哈希表累加 s 中每个字符出现的次数,再减去 t 中对应的每个字符出现的次数。遍历结束后,若字符中出现次数不为 0 的情况,返回 false,否则返回 true。
54
+
54
55
<!-- tabs:start -->
55
56
56
57
### ** Python3**
57
58
58
59
<!-- 这里可写当前语言的特殊实现逻辑 -->
59
60
60
61
``` python
61
-
62
+ class Solution :
63
+ def isAnagram (self , s : str , t : str ) -> bool :
64
+ if len (s) != len (t) or s == t:
65
+ return False
66
+ n = len (s)
67
+ chars = [0 ] * 26
68
+ for i in range (n):
69
+ chars[ord (s[i]) - ord (' a' )] += 1
70
+ chars[ord (t[i]) - ord (' a' )] -= 1
71
+ for c in chars:
72
+ if c != 0 :
73
+ return False
74
+ return True
62
75
```
63
76
64
77
### ** Java**
65
78
66
79
<!-- 这里可写当前语言的特殊实现逻辑 -->
67
80
68
81
``` java
82
+ class Solution {
83
+ public boolean isAnagram (String s , String t ) {
84
+ int n;
85
+ if ((n = s. length()) != t. length() || (Objects . equals(s, t))) {
86
+ return false ;
87
+ }
88
+ int [] chars = new int [26 ];
89
+ for (int i = 0 ; i < n; ++ i) {
90
+ ++ chars[s. charAt(i) - ' a' ];
91
+ -- chars[t. charAt(i) - ' a' ];
92
+ }
93
+ for (int c : chars) {
94
+ if (c != 0 ) {
95
+ return false ;
96
+ }
97
+ }
98
+ return true ;
99
+ }
100
+ }
101
+ ```
102
+
103
+ ### ** C++**
104
+
105
+ ``` cpp
106
+ class Solution {
107
+ public:
108
+ bool isAnagram(string s, string t) {
109
+ if (s.size() != t.size() || s == t)
110
+ return false;
111
+ vector<int > chars(26, 0);
112
+ for (int i = 0, n = s.size(); i < n; ++i)
113
+ {
114
+ ++chars[ s[ i] - 'a'] ;
115
+ --chars[ t[ i] - 'a'] ;
116
+ }
117
+ for (int c : chars)
118
+ {
119
+ if (c != 0)
120
+ return false;
121
+ }
122
+ return true;
123
+ }
124
+ };
125
+ ```
69
126
127
+ ### **Go**
128
+
129
+ ```go
130
+ func isAnagram(s string, t string) bool {
131
+ if len(s) != len(t) || s == t {
132
+ return false
133
+ }
134
+ var chars [26]int
135
+ for i := 0; i < len(s); i++ {
136
+ chars[s[i]-'a']++
137
+ chars[t[i]-'a']--
138
+ }
139
+ for _, c := range chars {
140
+ if c != 0 {
141
+ return false
142
+ }
143
+ }
144
+ return true
145
+ }
70
146
```
71
147
72
148
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool isAnagram (string s, string t) {
4
+ if (s.size () != t.size () || s == t)
5
+ return false ;
6
+ vector<int > chars (26 , 0 );
7
+ for (int i = 0 , n = s.size (); i < n; ++i)
8
+ {
9
+ ++chars[s[i] - ' a' ];
10
+ --chars[t[i] - ' a' ];
11
+ }
12
+ for (int c : chars)
13
+ {
14
+ if (c != 0 )
15
+ return false ;
16
+ }
17
+ return true ;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ func isAnagram (s string , t string ) bool {
2
+ if len (s ) != len (t ) || s == t {
3
+ return false
4
+ }
5
+ var chars [26 ]int
6
+ for i := 0 ; i < len (s ); i ++ {
7
+ chars [s [i ]- 'a' ]++
8
+ chars [t [i ]- 'a' ]--
9
+ }
10
+ for _ , c := range chars {
11
+ if c != 0 {
12
+ return false
13
+ }
14
+ }
15
+ return true
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean isAnagram (String s , String t ) {
3
+ int n ;
4
+ if ((n = s .length ()) != t .length () || (Objects .equals (s , t ))) {
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 c : chars ) {
13
+ if (c != 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 ) or s == 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 c in chars :
11
+ if c != 0 :
12
+ return False
13
+ return True
Original file line number Diff line number Diff line change 30
30
31
31
<!-- 这里可写通用的实现逻辑 -->
32
32
33
- 哈希表解决 。
33
+ 数组或哈希表累加 s 中每个字符出现的次数,再减去 t 中对应的每个字符出现的次数。遍历结束后,若字符中出现次数不为 0 的情况,返回 false,否则返回 true 。
34
34
35
35
<!-- tabs:start -->
36
36
@@ -48,8 +48,8 @@ class Solution:
48
48
for i in range (n):
49
49
chars[ord (s[i]) - ord (' a' )] += 1
50
50
chars[ord (t[i]) - ord (' a' )] -= 1
51
- for i in range ( 26 ) :
52
- if chars[i] != 0 :
51
+ for c in chars :
52
+ if c != 0 :
53
53
return False
54
54
return True
55
55
```
@@ -70,8 +70,8 @@ class Solution {
70
70
++ chars[s. charAt(i) - ' a' ];
71
71
-- chars[t. charAt(i) - ' a' ];
72
72
}
73
- for (int i = 0 ; i < 26 ; ++ i ) {
74
- if (chars[i] != 0 ) {
73
+ for (int c : chars ) {
74
+ if (c != 0 ) {
75
75
return false ;
76
76
}
77
77
}
@@ -101,18 +101,18 @@ function isAnagram(s: string, t: string): boolean {
101
101
class Solution {
102
102
public:
103
103
bool isAnagram(string s, string t) {
104
- if (s.size() != t.size()) {
104
+ if (s.size() != t.size())
105
105
return false;
106
- }
107
106
vector<int > chars(26, 0);
108
- for (int i = 0, n = s.size(); i < n; ++i) {
107
+ for (int i = 0, n = s.size(); i < n; ++i)
108
+ {
109
109
++chars[ s[ i] - 'a'] ;
110
110
--chars[ t[ i] - 'a'] ;
111
111
}
112
- for (int i = 0; i < 26; ++i) {
113
- if (chars[ i] != 0) {
112
+ for (int c : chars)
113
+ {
114
+ if (c != 0)
114
115
return false;
115
- }
116
116
}
117
117
return true;
118
118
}
@@ -131,8 +131,8 @@ func isAnagram(s string, t string) bool {
131
131
chars[s[i]-'a']++
132
132
chars[t[i]-'a']--
133
133
}
134
- for i := 0; i < 26; i++ {
135
- if chars[i] != 0 {
134
+ for _, c := range chars {
135
+ if c != 0 {
136
136
return false
137
137
}
138
138
}
Original file line number Diff line number Diff line change @@ -42,8 +42,8 @@ class Solution:
42
42
for i in range (n):
43
43
chars[ord (s[i]) - ord (' a' )] += 1
44
44
chars[ord (t[i]) - ord (' a' )] -= 1
45
- for i in range ( 26 ) :
46
- if chars[i] != 0 :
45
+ for c in chars :
46
+ if c != 0 :
47
47
return False
48
48
return True
49
49
```
@@ -62,8 +62,8 @@ class Solution {
62
62
++ chars[s. charAt(i) - ' a' ];
63
63
-- chars[t. charAt(i) - ' a' ];
64
64
}
65
- for (int i = 0 ; i < 26 ; ++ i ) {
66
- if (chars[i] != 0 ) {
65
+ for (int c : chars ) {
66
+ if (c != 0 ) {
67
67
return false ;
68
68
}
69
69
}
@@ -93,18 +93,18 @@ function isAnagram(s: string, t: string): boolean {
93
93
class Solution {
94
94
public:
95
95
bool isAnagram(string s, string t) {
96
- if (s.size() != t.size()) {
96
+ if (s.size() != t.size())
97
97
return false;
98
- }
99
98
vector<int > chars(26, 0);
100
- for (int i = 0, n = s.size(); i < n; ++i) {
99
+ for (int i = 0, n = s.size(); i < n; ++i)
100
+ {
101
101
++chars[ s[ i] - 'a'] ;
102
102
--chars[ t[ i] - 'a'] ;
103
103
}
104
- for (int i = 0; i < 26; ++i) {
105
- if (chars[ i] != 0) {
104
+ for (int c : chars)
105
+ {
106
+ if (c != 0)
106
107
return false;
107
- }
108
108
}
109
109
return true;
110
110
}
@@ -123,8 +123,8 @@ func isAnagram(s string, t string) bool {
123
123
chars[s[i]-'a']++
124
124
chars[t[i]-'a']--
125
125
}
126
- for i := 0; i < 26; i++ {
127
- if chars[i] != 0 {
126
+ for _, c := range chars {
127
+ if c != 0 {
128
128
return false
129
129
}
130
130
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
bool isAnagram (string s, string t) {
4
- if (s.size () != t.size ()) {
4
+ if (s.size () != t.size ())
5
5
return false ;
6
- }
7
6
vector<int > chars (26 , 0 );
8
- for (int i = 0 , n = s.size (); i < n; ++i) {
7
+ for (int i = 0 , n = s.size (); i < n; ++i)
8
+ {
9
9
++chars[s[i] - ' a' ];
10
10
--chars[t[i] - ' a' ];
11
11
}
12
- for (int i = 0 ; i < 26 ; ++i) {
13
- if (chars[i] != 0 ) {
12
+ for (int c : chars)
13
+ {
14
+ if (c != 0 )
14
15
return false ;
15
- }
16
16
}
17
17
return true ;
18
18
}
Original file line number Diff line number Diff line change @@ -7,8 +7,8 @@ func isAnagram(s string, t string) bool {
7
7
chars [s [i ]- 'a' ]++
8
8
chars [t [i ]- 'a' ]--
9
9
}
10
- for i := 0 ; i < 26 ; i ++ {
11
- if chars [ i ] != 0 {
10
+ for _ , c := range chars {
11
+ if c != 0 {
12
12
return false
13
13
}
14
14
}
Original file line number Diff line number Diff line change @@ -9,8 +9,8 @@ public boolean isAnagram(String s, String t) {
9
9
++chars [s .charAt (i ) - 'a' ];
10
10
--chars [t .charAt (i ) - 'a' ];
11
11
}
12
- for (int i = 0 ; i < 26 ; ++ i ) {
13
- if (chars [ i ] != 0 ) {
12
+ for (int c : chars ) {
13
+ if (c != 0 ) {
14
14
return false ;
15
15
}
16
16
}
You can’t perform that action at this time.
0 commit comments