Skip to content

Commit f732cbd

Browse files
committed
feat: add typescript solution to lc problem: No.0438
No.0438.Find All Anagrams in a String
1 parent e23e9d2 commit f732cbd

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

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

+25
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,31 @@ class Solution {
139139
}
140140
```
141141

142+
### **TypeScript**
143+
144+
```ts
145+
function findAnagrams(s: string, p: string): number[] {
146+
let n = s.length, m = p.length;
147+
let cnt = new Array(26).fill(0);
148+
let ans = [];
149+
for (let i = 0; i < m; i++) {
150+
cnt[p.charCodeAt(i) - 97]--;
151+
cnt[s.charCodeAt(i) - 97]++;
152+
}
153+
if (cnt.every(v => v == 0)) {
154+
ans.push(0);
155+
}
156+
for (let i = m; i < n; i++) {
157+
cnt[s.charCodeAt(i) - 97]++;
158+
cnt[s.charCodeAt(i - m) - 97]--;
159+
if (cnt.every(v => v == 0)) {
160+
ans.push(i - m + 1);
161+
}
162+
}
163+
return ans;
164+
};
165+
```
166+
142167
### **C++**
143168

144169
```cpp

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

+25
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,31 @@ class Solution {
8989
}
9090
```
9191

92+
### **TypeScript**
93+
94+
```ts
95+
function findAnagrams(s: string, p: string): number[] {
96+
let n = s.length, m = p.length;
97+
let cnt = new Array(26).fill(0);
98+
let ans = [];
99+
for (let i = 0; i < m; i++) {
100+
cnt[p.charCodeAt(i) - 97]--;
101+
cnt[s.charCodeAt(i) - 97]++;
102+
}
103+
if (cnt.every(v => v == 0)) {
104+
ans.push(0);
105+
}
106+
for (let i = m; i < n; i++) {
107+
cnt[s.charCodeAt(i) - 97]++;
108+
cnt[s.charCodeAt(i - m) - 97]--;
109+
if (cnt.every(v => v == 0)) {
110+
ans.push(i - m + 1);
111+
}
112+
}
113+
return ans;
114+
};
115+
```
116+
92117
### **C++**
93118

94119
```cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function findAnagrams(s: string, p: string): number[] {
2+
let n = s.length, m = p.length;
3+
let cnt = new Array(26).fill(0);
4+
let ans = [];
5+
for (let i = 0; i < m; i++) {
6+
cnt[p.charCodeAt(i) - 97]--;
7+
cnt[s.charCodeAt(i) - 97]++;
8+
}
9+
if (cnt.every(v => v == 0)) {
10+
ans.push(0);
11+
}
12+
for (let i = m; i < n; i++) {
13+
cnt[s.charCodeAt(i) - 97]++;
14+
cnt[s.charCodeAt(i - m) - 97]--;
15+
if (cnt.every(v => v == 0)) {
16+
ans.push(i - m + 1);
17+
}
18+
}
19+
return ans;
20+
};

0 commit comments

Comments
 (0)