Skip to content

Commit 41efb7a

Browse files
committed
feat: add new lc problems and update solutions
* Update solutions to lc problems: No.0242,0266,0438 * Add new lc problems: No.2084,2085,2086,2087,2088
1 parent 25a4447 commit 41efb7a

File tree

41 files changed

+1185
-170
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1185
-170
lines changed

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

+25-9
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,11 @@ class Solution:
4343
def isAnagram(self, s: str, t: str) -> bool:
4444
if len(s) != len(t):
4545
return False
46-
n = len(s)
4746
chars = [0] * 26
48-
for i in range(n):
47+
for i in range(len(s)):
4948
chars[ord(s[i]) - ord('a')] += 1
5049
chars[ord(t[i]) - ord('a')] -= 1
51-
for c in chars:
52-
if c != 0:
53-
return False
54-
return True
50+
return all(c == 0 for c in chars)
5551
```
5652

5753
### **Java**
@@ -61,12 +57,11 @@ class Solution:
6157
```java
6258
class Solution {
6359
public boolean isAnagram(String s, String t) {
64-
int n;
65-
if ((n = s.length()) != t.length()) {
60+
if (s.length() != t.length()) {
6661
return false;
6762
}
6863
int[] chars = new int[26];
69-
for (int i = 0; i < n; ++i) {
64+
for (int i = 0; i < s.length(); ++i) {
7065
++chars[s.charAt(i) - 'a'];
7166
--chars[t.charAt(i) - 'a'];
7267
}
@@ -140,6 +135,27 @@ func isAnagram(s string, t string) bool {
140135
}
141136
```
142137

138+
139+
### **JavaScript**
140+
141+
```js
142+
/**
143+
* @param {string} s
144+
* @param {string} t
145+
* @return {boolean}
146+
*/
147+
var isAnagram = function(s, t) {
148+
if (s.length != t.length) return false;
149+
let record = new Array(26).fill(0);
150+
let base = 'a'.charCodeAt(0);
151+
for (let i = 0; i < s.length; ++i) {
152+
++record[s.charCodeAt(i) - base];
153+
--record[t.charCodeAt(i) - base];
154+
}
155+
return record.every(v => v == 0);
156+
};
157+
```
158+
143159
### **...**
144160

145161
```

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

+24-9
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,23 @@ class Solution:
3737
def isAnagram(self, s: str, t: str) -> bool:
3838
if len(s) != len(t):
3939
return False
40-
n = len(s)
4140
chars = [0] * 26
42-
for i in range(n):
41+
for i in range(len(s)):
4342
chars[ord(s[i]) - ord('a')] += 1
4443
chars[ord(t[i]) - ord('a')] -= 1
45-
for c in chars:
46-
if c != 0:
47-
return False
48-
return True
44+
return all(c == 0 for c in chars)
4945
```
5046

5147
### **Java**
5248

5349
```java
5450
class Solution {
5551
public boolean isAnagram(String s, String t) {
56-
int n;
57-
if ((n = s.length()) != t.length()) {
52+
if (s.length() != t.length()) {
5853
return false;
5954
}
6055
int[] chars = new int[26];
61-
for (int i = 0; i < n; ++i) {
56+
for (int i = 0; i < s.length(); ++i) {
6257
++chars[s.charAt(i) - 'a'];
6358
--chars[t.charAt(i) - 'a'];
6459
}
@@ -132,6 +127,26 @@ func isAnagram(s string, t string) bool {
132127
}
133128
```
134129

130+
### **JavaScript**
131+
132+
```js
133+
/**
134+
* @param {string} s
135+
* @param {string} t
136+
* @return {boolean}
137+
*/
138+
var isAnagram = function(s, t) {
139+
if (s.length != t.length) return false;
140+
let record = new Array(26).fill(0);
141+
let base = 'a'.charCodeAt(0);
142+
for (let i = 0; i < s.length; ++i) {
143+
++record[s.charCodeAt(i) - base];
144+
--record[t.charCodeAt(i) - base];
145+
}
146+
return record.every(v => v == 0);
147+
};
148+
```
149+
135150
### **...**
136151

137152
```

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
class Solution {
22
public boolean isAnagram(String s, String t) {
3-
int n;
4-
if ((n = s.length()) != t.length()) {
3+
if (s.length() != t.length()) {
54
return false;
65
}
76
int[] chars = new int[26];
8-
for (int i = 0; i < n; ++i) {
7+
for (int i = 0; i < s.length(); ++i) {
98
++chars[s.charAt(i) - 'a'];
109
--chars[t.charAt(i) - 'a'];
1110
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
1-
const isAnagram = function (s, t) {
2-
let a = {},
3-
b = {};
4-
for (let i = 0; i < s.length; i++) {
5-
if (a.hasOwnProperty(s[i])) {
6-
a[s[i]]++;
7-
} else {
8-
a[s[i]] = 1;
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
var isAnagram = function(s, t) {
7+
if (s.length != t.length) return false;
8+
let record = new Array(26).fill(0);
9+
let base = 'a'.charCodeAt(0);
10+
for (let i = 0; i < s.length; ++i) {
11+
++record[s.charCodeAt(i) - base];
12+
--record[t.charCodeAt(i) - base];
913
}
10-
}
11-
for (let i = 0; i < t.length; i++) {
12-
if (b.hasOwnProperty(t[i])) {
13-
b[t[i]]++;
14-
} else {
15-
b[t[i]] = 1;
16-
}
17-
}
18-
let keyA = Object.keys(a);
19-
let keyB = Object.keys(b);
20-
if (keyA.length !== keyB.length) {
21-
return false;
22-
}
23-
for (let i = 0; i < keyA.length; i++) {
24-
if (a[keyA[i]] !== b[keyA[i]]) return false;
25-
}
26-
return true;
27-
};
14+
return record.every(v => v == 0);
15+
};

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ class Solution:
22
def isAnagram(self, s: str, t: str) -> bool:
33
if len(s) != len(t):
44
return False
5-
n = len(s)
65
chars = [0] * 26
7-
for i in range(n):
6+
for i in range(len(s)):
87
chars[ord(s[i]) - ord('a')] += 1
98
chars[ord(t[i]) - ord('a')] -= 1
10-
for c in chars:
11-
if c != 0:
12-
return False
13-
return True
9+
return all(c == 0 for c in chars)

solution/0200-0299/0266.Palindrome Permutation/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@
3838
```python
3939
class Solution:
4040
def canPermutePalindrome(self, s: str) -> bool:
41-
counter = collections.Counter(s)
42-
odd_cnt = sum(e % 2 for e in counter.values())
43-
return odd_cnt < 2
41+
counter = Counter(s)
42+
return sum(e % 2 for e in counter.values()) < 2
4443
```
4544

4645
### **Java**

solution/0200-0299/0266.Palindrome Permutation/README_EN.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@
4545
```python
4646
class Solution:
4747
def canPermutePalindrome(self, s: str) -> bool:
48-
counter = collections.Counter(s)
49-
odd_cnt = sum(e % 2 for e in counter.values())
50-
return odd_cnt < 2
48+
counter = Counter(s)
49+
return sum(e % 2 for e in counter.values()) < 2
5150
```
5251

5352
### **Java**
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Solution:
22
def canPermutePalindrome(self, s: str) -> bool:
3-
counter = collections.Counter(s)
4-
odd_cnt = sum(e % 2 for e in counter.values())
5-
return odd_cnt < 2
3+
counter = Counter(s)
4+
return sum(e % 2 for e in counter.values()) < 2

solution/0400-0499/0438.Find All Anagrams in a String/README.md

+73-18
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,19 @@ s: &quot;abab&quot; p: &quot;ab&quot;
6363
```python
6464
class Solution:
6565
def findAnagrams(self, s: str, p: str) -> List[int]:
66-
counter = collections.Counter(p)
67-
res = []
66+
counter = Counter(p)
67+
ans = []
6868
left = right = 0
69-
t = collections.Counter()
69+
t = Counter()
7070
while right < len(s):
7171
t[s[right]] += 1
7272
while t[s[right]] > counter[s[right]]:
7373
t[s[left]] -= 1
7474
left += 1
75-
if right - left == len(p) - 1:
76-
res.append(left)
75+
if right - left + 1 == len(p):
76+
ans.append(left)
7777
right += 1
78-
return res
78+
return ans
7979
```
8080

8181
### **Java**
@@ -88,11 +88,11 @@ class Solution:
8888
class Solution {
8989
public List<Integer> findAnagrams(String s, String p) {
9090
int[] counter = new int[26];
91-
for (int i = 0; i < p.length(); ++i) {
92-
++counter[p.charAt(i) - 'a'];
91+
for (char c : p.toCharArray()) {
92+
++counter[c - 'a'];
9393
}
94-
List<Integer> res = new ArrayList<>();
95-
for (int i = 0; i <= s.length() - p.length(); ++i) {
94+
List<Integer> ans = new ArrayList<>();
95+
for (int i = 0; i + p.length() - 1 < s.length(); ++i) {
9696
int[] t = Arrays.copyOf(counter, counter.length);
9797
boolean find = true;
9898
for (int j = i; j < i + p.length(); ++j) {
@@ -102,10 +102,10 @@ class Solution {
102102
}
103103
}
104104
if (find) {
105-
res.add(i);
105+
ans.add(i);
106106
}
107107
}
108-
return res;
108+
return ans;
109109
}
110110
}
111111
```
@@ -116,10 +116,10 @@ class Solution {
116116
class Solution {
117117
public List<Integer> findAnagrams(String s, String p) {
118118
int[] counter = new int[26];
119-
for (int i = 0; i < p.length(); ++i) {
120-
++counter[p.charAt(i) - 'a'];
119+
for (char c : p.toCharArray()) {
120+
++counter[c - 'a'];
121121
}
122-
List<Integer> res = new ArrayList<>();
122+
List<Integer> ans = new ArrayList<>();
123123
int left = 0, right = 0;
124124
int[] t = new int[26];
125125
while (right < s.length()) {
@@ -129,16 +129,71 @@ class Solution {
129129
--t[s.charAt(left) - 'a'];
130130
++left;
131131
}
132-
if (right - left == p.length() - 1) {
133-
res.add(left);
132+
if (right - left + 1 == p.length()) {
133+
ans.add(left);
134134
}
135135
++right;
136136
}
137-
return res;
137+
return ans;
138138
}
139139
}
140140
```
141141

142+
### **C++**
143+
144+
```cpp
145+
class Solution {
146+
public:
147+
vector<int> findAnagrams(string s, string p) {
148+
vector<int> counter(26);
149+
for (char c : p) ++counter[c - 'a'];
150+
vector<int> ans;
151+
int left = 0, right = 0;
152+
vector<int> t(26);
153+
while (right < s.size())
154+
{
155+
int i = s[right] - 'a';
156+
++t[i];
157+
while (t[i] > counter[i])
158+
{
159+
--t[s[left] - 'a'];
160+
++left;
161+
}
162+
if (right - left + 1 == p.size()) ans.push_back(left);
163+
++right;
164+
}
165+
return ans;
166+
}
167+
};
168+
```
169+
170+
### **Go**
171+
172+
```go
173+
func findAnagrams(s string, p string) []int {
174+
counter := make([]int, 26)
175+
for _, c := range p {
176+
counter[c-'a']++
177+
}
178+
var ans []int
179+
left, right := 0, 0
180+
t := make([]int, 26)
181+
for right < len(s) {
182+
i := s[right] - 'a'
183+
t[i]++
184+
for t[i] > counter[i] {
185+
t[s[left]-'a']--
186+
left++
187+
}
188+
if right-left+1 == len(p) {
189+
ans = append(ans, left)
190+
}
191+
right++
192+
}
193+
return ans
194+
}
195+
```
196+
142197
### **...**
143198

144199
```

0 commit comments

Comments
 (0)