Skip to content

Commit f8e49aa

Browse files
committed
feat: add solutions to lc problem: No.0438
No.0438.Find All Anagrams in a String
1 parent 036f2bf commit f8e49aa

File tree

9 files changed

+676
-320
lines changed

9 files changed

+676
-320
lines changed

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

+263-111
Large diffs are not rendered by default.

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

+259-89
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
class Solution {
2-
public:
3-
vector<int> findAnagrams(string s, string p) {
4-
vector<int> counter(26);
5-
for (char c : p) ++counter[c - 'a'];
6-
vector<int> ans;
7-
int left = 0, right = 0;
8-
vector<int> t(26);
9-
while (right < s.size()) {
10-
int i = s[right] - 'a';
11-
++t[i];
12-
while (t[i] > counter[i]) {
13-
--t[s[left] - 'a'];
14-
++left;
15-
}
16-
if (right - left + 1 == p.size()) ans.push_back(left);
17-
++right;
18-
}
19-
return ans;
20-
}
1+
class Solution {
2+
public:
3+
vector<int> findAnagrams(string s, string p) {
4+
int m = s.size(), n = p.size();
5+
vector<int> ans;
6+
if (m < n) {
7+
return ans;
8+
}
9+
vector<int> cnt1(26);
10+
for (char& c : p) {
11+
++cnt1[c - 'a'];
12+
}
13+
vector<int> cnt2(26);
14+
for (int i = 0; i < n - 1; ++i) {
15+
++cnt2[s[i] - 'a'];
16+
}
17+
for (int i = n - 1; i < m; ++i) {
18+
++cnt2[s[i] - 'a'];
19+
if (cnt1 == cnt2) {
20+
ans.push_back(i - n + 1);
21+
}
22+
--cnt2[s[i - n + 1] - 'a'];
23+
}
24+
return ans;
25+
}
2126
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public IList<int> FindAnagrams(string s, string p) {
3+
int m = s.Length, n = p.Length;
4+
IList<int> ans = new List<int>();
5+
if (m < n) {
6+
return ans;
7+
}
8+
int[] cnt1 = new int[26];
9+
int[] cnt2 = new int[26];
10+
for (int i = 0; i < n; ++i) {
11+
++cnt1[p[i] - 'a'];
12+
}
13+
for (int i = 0, j = 0; i < m; ++i) {
14+
int k = s[i] - 'a';
15+
++cnt2[k];
16+
while (cnt2[k] > cnt1[k]) {
17+
--cnt2[s[j++] - 'a'];
18+
}
19+
if (i - j + 1 == n) {
20+
ans.Add(j);
21+
}
22+
}
23+
return ans;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
func findAnagrams(s string, p string) []int {
2-
counter := make([]int, 26)
3-
for _, c := range p {
4-
counter[c-'a']++
5-
}
6-
var ans []int
7-
left, right := 0, 0
8-
t := make([]int, 26)
9-
for right < len(s) {
10-
i := s[right] - 'a'
11-
t[i]++
12-
for t[i] > counter[i] {
13-
t[s[left]-'a']--
14-
left++
15-
}
16-
if right-left+1 == len(p) {
17-
ans = append(ans, left)
18-
}
19-
right++
20-
}
21-
return ans
1+
func findAnagrams(s string, p string) (ans []int) {
2+
m, n := len(s), len(p)
3+
if m < n {
4+
return
5+
}
6+
cnt1 := [26]int{}
7+
cnt2 := [26]int{}
8+
for _, c := range p {
9+
cnt1[c-'a']++
10+
}
11+
for _, c := range s[:n-1] {
12+
cnt2[c-'a']++
13+
}
14+
for i := n - 1; i < m; i++ {
15+
cnt2[s[i]-'a']++
16+
if cnt1 == cnt2 {
17+
ans = append(ans, i-n+1)
18+
}
19+
cnt2[s[i-n+1]-'a']--
20+
}
21+
return
2222
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
class Solution {
2-
public List<Integer> findAnagrams(String s, String p) {
3-
int[] counter = new int[26];
4-
for (char c : p.toCharArray()) {
5-
++counter[c - 'a'];
6-
}
7-
List<Integer> ans = new ArrayList<>();
8-
int left = 0, right = 0;
9-
int[] t = new int[26];
10-
while (right < s.length()) {
11-
int i = s.charAt(right) - 'a';
12-
++t[i];
13-
while (t[i] > counter[i]) {
14-
--t[s.charAt(left) - 'a'];
15-
++left;
16-
}
17-
if (right - left + 1 == p.length()) {
18-
ans.add(left);
19-
}
20-
++right;
21-
}
22-
return ans;
23-
}
1+
class Solution {
2+
public List<Integer> findAnagrams(String s, String p) {
3+
int m = s.length(), n = p.length();
4+
List<Integer> ans = new ArrayList<>();
5+
if (m < n) {
6+
return ans;
7+
}
8+
int[] cnt1 = new int[26];
9+
for (int i = 0; i < n; ++i) {
10+
++cnt1[p.charAt(i) - 'a'];
11+
}
12+
int[] cnt2 = new int[26];
13+
for (int i = 0; i < n - 1; ++i) {
14+
++cnt2[s.charAt(i) - 'a'];
15+
}
16+
for (int i = n - 1; i < m; ++i) {
17+
++cnt2[s.charAt(i) - 'a'];
18+
if (Arrays.equals(cnt1, cnt2)) {
19+
ans.add(i - n + 1);
20+
}
21+
--cnt2[s.charAt(i - n + 1) - 'a'];
22+
}
23+
return ans;
24+
}
2425
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
class Solution:
2-
def findAnagrams(self, s: str, p: str) -> List[int]:
3-
counter = Counter(p)
4-
ans = []
5-
left = right = 0
6-
t = Counter()
7-
while right < len(s):
8-
t[s[right]] += 1
9-
while t[s[right]] > counter[s[right]]:
10-
t[s[left]] -= 1
11-
left += 1
12-
if right - left + 1 == len(p):
13-
ans.append(left)
14-
right += 1
15-
return ans
1+
class Solution:
2+
def findAnagrams(self, s: str, p: str) -> List[int]:
3+
m, n = len(s), len(p)
4+
ans = []
5+
if m < n:
6+
return ans
7+
cnt1 = Counter(p)
8+
cnt2 = Counter(s[: n - 1])
9+
for i in range(n - 1, m):
10+
cnt2[s[i]] += 1
11+
if cnt1 == cnt2:
12+
ans.append(i - n + 1)
13+
cnt2[s[i - n + 1]] -= 1
14+
return ans
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
impl Solution {
2-
pub fn find_anagrams(s: String, p: String) -> Vec<i32> {
3-
let (s, p) = (s.as_bytes(), p.as_bytes());
4-
let (m, n) = (s.len(), p.len());
5-
let mut res = vec![];
6-
if n > m {
7-
return res;
8-
}
9-
10-
let mut counter = [0; 26];
11-
for i in 0..n {
12-
counter[(p[i] - b'a') as usize] += 1;
13-
counter[(s[i] - b'a') as usize] -= 1;
14-
}
15-
for i in n..m {
16-
if counter.iter().all(|&v| v == 0) {
17-
res.push((i - n) as i32);
18-
}
19-
counter[(s[i] - b'a') as usize] -= 1;
20-
counter[(s[i - n] - b'a') as usize] += 1;
21-
}
22-
if counter.iter().all(|&v| v == 0) {
23-
res.push((m - n) as i32);
24-
}
25-
res
26-
}
27-
}
1+
impl Solution {
2+
pub fn find_anagrams(s: String, p: String) -> Vec<i32> {
3+
let (s, p) = (s.as_bytes(), p.as_bytes());
4+
let (m, n) = (s.len(), p.len());
5+
let mut ans = vec![];
6+
if m < n {
7+
return ans;
8+
}
9+
10+
let mut cnt = [0; 26];
11+
for i in 0..n {
12+
cnt[(p[i] - b'a') as usize] += 1;
13+
cnt[(s[i] - b'a') as usize] -= 1;
14+
}
15+
for i in n..m {
16+
if cnt.iter().all(|&v| v == 0) {
17+
ans.push((i - n) as i32);
18+
}
19+
cnt[(s[i] - b'a') as usize] -= 1;
20+
cnt[(s[i - n] - b'a') as usize] += 1;
21+
}
22+
if cnt.iter().all(|&v| v == 0) {
23+
ans.push((m - n) as i32);
24+
}
25+
ans
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
function findAnagrams(s: string, p: string): number[] {
2-
let n = s.length,
3-
m = p.length;
4-
let cnt = new Array(26).fill(0);
5-
let ans = [];
6-
for (let i = 0; i < m; i++) {
7-
cnt[p.charCodeAt(i) - 97]--;
8-
cnt[s.charCodeAt(i) - 97]++;
2+
const m = s.length;
3+
const n = p.length;
4+
const ans: number[] = [];
5+
if (m < n) {
6+
return ans;
97
}
10-
if (cnt.every(v => v == 0)) {
11-
ans.push(0);
8+
const cnt1: number[] = new Array(26).fill(0);
9+
const cnt2: number[] = new Array(26).fill(0);
10+
const idx = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
11+
for (const c of p) {
12+
++cnt1[idx(c)];
1213
}
13-
for (let i = m; i < n; i++) {
14-
cnt[s.charCodeAt(i) - 97]++;
15-
cnt[s.charCodeAt(i - m) - 97]--;
16-
if (cnt.every(v => v == 0)) {
17-
ans.push(i - m + 1);
14+
for (const c of s.slice(0, n - 1)) {
15+
++cnt2[idx(c)];
16+
}
17+
for (let i = n - 1; i < m; ++i) {
18+
++cnt2[idx(s[i])];
19+
if (cnt1.toString() === cnt2.toString()) {
20+
ans.push(i - n + 1);
1821
}
22+
--cnt2[idx(s[i - n + 1])];
1923
}
2024
return ans;
2125
}

0 commit comments

Comments
 (0)