Skip to content

Commit 17ab2a2

Browse files
committed
feat: update leetcode solutions: No.0242
1 parent e514260 commit 17ab2a2

File tree

4 files changed

+97
-14
lines changed

4 files changed

+97
-14
lines changed

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

+34-2
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,54 @@
2828

2929
<!-- 这里可写通用的实现逻辑 -->
3030

31+
哈希表解决。
32+
3133
<!-- tabs:start -->
3234

3335
### **Python3**
3436

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

3739
```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
3953
```
4054

4155
### **Java**
4256

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

4559
```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+
}
4779
```
4880

4981
### **...**

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

+32-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,43 @@ What if the inputs contain unicode characters? How would you adapt your solution
4141
### **Python3**
4242

4343
```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
4557
```
4658

4759
### **Java**
4860

4961
```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+
}
5181
```
5282

5383
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
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 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):
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

0 commit comments

Comments
 (0)