Skip to content

Commit 1ca39ab

Browse files
committed
feat: add solutions to lcof2 problem: No.032
1 parent 841e15d commit 1ca39ab

File tree

11 files changed

+182
-39
lines changed

11 files changed

+182
-39
lines changed

lcof2/剑指 Offer II 032. 有效的变位词/README.md

+78-2
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,103 @@
4646

4747
<p><meta charset="UTF-8" />注意:本题与主站 242&nbsp;题相似(字母异位词定义不同):<a href="https://leetcode-cn.com/problems/valid-anagram/">https://leetcode-cn.com/problems/valid-anagram/</a></p>
4848

49-
5049
## 解法
5150

5251
<!-- 这里可写通用的实现逻辑 -->
5352

53+
数组或哈希表累加 s 中每个字符出现的次数,再减去 t 中对应的每个字符出现的次数。遍历结束后,若字符中出现次数不为 0 的情况,返回 false,否则返回 true。
54+
5455
<!-- tabs:start -->
5556

5657
### **Python3**
5758

5859
<!-- 这里可写当前语言的特殊实现逻辑 -->
5960

6061
```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
6275
```
6376

6477
### **Java**
6578

6679
<!-- 这里可写当前语言的特殊实现逻辑 -->
6780

6881
```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+
```
69126
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+
}
70146
```
71147

72148
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

solution/0200-0299/0242.Valid Anagram/README.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
<!-- 这里可写通用的实现逻辑 -->
3232

33-
哈希表解决
33+
数组或哈希表累加 s 中每个字符出现的次数,再减去 t 中对应的每个字符出现的次数。遍历结束后,若字符中出现次数不为 0 的情况,返回 false,否则返回 true
3434

3535
<!-- tabs:start -->
3636

@@ -48,8 +48,8 @@ class Solution:
4848
for i in range(n):
4949
chars[ord(s[i]) - ord('a')] += 1
5050
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:
5353
return False
5454
return True
5555
```
@@ -70,8 +70,8 @@ class Solution {
7070
++chars[s.charAt(i) - 'a'];
7171
--chars[t.charAt(i) - 'a'];
7272
}
73-
for (int i = 0; i < 26; ++i) {
74-
if (chars[i] != 0) {
73+
for (int c : chars) {
74+
if (c != 0) {
7575
return false;
7676
}
7777
}
@@ -101,18 +101,18 @@ function isAnagram(s: string, t: string): boolean {
101101
class Solution {
102102
public:
103103
bool isAnagram(string s, string t) {
104-
if (s.size() != t.size()) {
104+
if (s.size() != t.size())
105105
return false;
106-
}
107106
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+
{
109109
++chars[s[i] - 'a'];
110110
--chars[t[i] - 'a'];
111111
}
112-
for (int i = 0; i < 26; ++i) {
113-
if (chars[i] != 0) {
112+
for (int c : chars)
113+
{
114+
if (c != 0)
114115
return false;
115-
}
116116
}
117117
return true;
118118
}
@@ -131,8 +131,8 @@ func isAnagram(s string, t string) bool {
131131
chars[s[i]-'a']++
132132
chars[t[i]-'a']--
133133
}
134-
for i := 0; i < 26; i++ {
135-
if chars[i] != 0 {
134+
for _, c := range chars {
135+
if c != 0 {
136136
return false
137137
}
138138
}

solution/0200-0299/0242.Valid Anagram/README_EN.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class Solution:
4242
for i in range(n):
4343
chars[ord(s[i]) - ord('a')] += 1
4444
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:
4747
return False
4848
return True
4949
```
@@ -62,8 +62,8 @@ class Solution {
6262
++chars[s.charAt(i) - 'a'];
6363
--chars[t.charAt(i) - 'a'];
6464
}
65-
for (int i = 0; i < 26; ++i) {
66-
if (chars[i] != 0) {
65+
for (int c : chars) {
66+
if (c != 0) {
6767
return false;
6868
}
6969
}
@@ -93,18 +93,18 @@ function isAnagram(s: string, t: string): boolean {
9393
class Solution {
9494
public:
9595
bool isAnagram(string s, string t) {
96-
if (s.size() != t.size()) {
96+
if (s.size() != t.size())
9797
return false;
98-
}
9998
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+
{
101101
++chars[s[i] - 'a'];
102102
--chars[t[i] - 'a'];
103103
}
104-
for (int i = 0; i < 26; ++i) {
105-
if (chars[i] != 0) {
104+
for (int c : chars)
105+
{
106+
if (c != 0)
106107
return false;
107-
}
108108
}
109109
return true;
110110
}
@@ -123,8 +123,8 @@ func isAnagram(s string, t string) bool {
123123
chars[s[i]-'a']++
124124
chars[t[i]-'a']--
125125
}
126-
for i := 0; i < 26; i++ {
127-
if chars[i] != 0 {
126+
for _, c := range chars {
127+
if c != 0 {
128128
return false
129129
}
130130
}

solution/0200-0299/0242.Valid Anagram/Solution.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
class Solution {
22
public:
33
bool isAnagram(string s, string t) {
4-
if (s.size() != t.size()) {
4+
if (s.size() != t.size())
55
return false;
6-
}
76
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+
{
99
++chars[s[i] - 'a'];
1010
--chars[t[i] - 'a'];
1111
}
12-
for (int i = 0; i < 26; ++i) {
13-
if (chars[i] != 0) {
12+
for (int c : chars)
13+
{
14+
if (c != 0)
1415
return false;
15-
}
1616
}
1717
return true;
1818
}

solution/0200-0299/0242.Valid Anagram/Solution.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ func isAnagram(s string, t string) bool {
77
chars[s[i]-'a']++
88
chars[t[i]-'a']--
99
}
10-
for i := 0; i < 26; i++ {
11-
if chars[i] != 0 {
10+
for _, c := range chars {
11+
if c != 0 {
1212
return false
1313
}
1414
}

solution/0200-0299/0242.Valid Anagram/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public boolean isAnagram(String s, String t) {
99
++chars[s.charAt(i) - 'a'];
1010
--chars[t.charAt(i) - 'a'];
1111
}
12-
for (int i = 0; i < 26; ++i) {
13-
if (chars[i] != 0) {
12+
for (int c : chars) {
13+
if (c != 0) {
1414
return false;
1515
}
1616
}

solution/0200-0299/0242.Valid Anagram/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def isAnagram(self, s: str, t: str) -> bool:
77
for i in range(n):
88
chars[ord(s[i]) - ord('a')] += 1
99
chars[ord(t[i]) - ord('a')] -= 1
10-
for i in range(26):
11-
if chars[i] != 0:
10+
for c in chars:
11+
if c != 0:
1212
return False
1313
return True

0 commit comments

Comments
 (0)