返回介绍

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

发布于 2024-06-17 01:04:00 字数 9054 浏览 0 评论 0 收藏 0

438. Find All Anagrams in a String

中文文档

Description

Given two strings s and p, return _an array of all the start indices of _p_'s anagrams in _s. You may return the answer in any order.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

 

Example 1:

Input: s = "cbaebabacd", p = "abc"
Output: [0,6]
Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".

Example 2:

Input: s = "abab", p = "ab"
Output: [0,1,2]
Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".

 

Constraints:

  • 1 <= s.length, p.length <= 3 * 104
  • s and p consist of lowercase English letters.

Solutions

Solution 1

class Solution:
  def findAnagrams(self, s: str, p: str) -> List[int]:
    m, n = len(s), len(p)
    ans = []
    if m < n:
      return ans
    cnt1 = Counter(p)
    cnt2 = Counter(s[: n - 1])
    for i in range(n - 1, m):
      cnt2[s[i]] += 1
      if cnt1 == cnt2:
        ans.append(i - n + 1)
      cnt2[s[i - n + 1]] -= 1
    return ans
class Solution {
  public List<Integer> findAnagrams(String s, String p) {
    int m = s.length(), n = p.length();
    List<Integer> ans = new ArrayList<>();
    if (m < n) {
      return ans;
    }
    int[] cnt1 = new int[26];
    for (int i = 0; i < n; ++i) {
      ++cnt1[p.charAt(i) - 'a'];
    }
    int[] cnt2 = new int[26];
    for (int i = 0; i < n - 1; ++i) {
      ++cnt2[s.charAt(i) - 'a'];
    }
    for (int i = n - 1; i < m; ++i) {
      ++cnt2[s.charAt(i) - 'a'];
      if (Arrays.equals(cnt1, cnt2)) {
        ans.add(i - n + 1);
      }
      --cnt2[s.charAt(i - n + 1) - 'a'];
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> findAnagrams(string s, string p) {
    int m = s.size(), n = p.size();
    vector<int> ans;
    if (m < n) {
      return ans;
    }
    vector<int> cnt1(26);
    for (char& c : p) {
      ++cnt1[c - 'a'];
    }
    vector<int> cnt2(26);
    for (int i = 0; i < n - 1; ++i) {
      ++cnt2[s[i] - 'a'];
    }
    for (int i = n - 1; i < m; ++i) {
      ++cnt2[s[i] - 'a'];
      if (cnt1 == cnt2) {
        ans.push_back(i - n + 1);
      }
      --cnt2[s[i - n + 1] - 'a'];
    }
    return ans;
  }
};
func findAnagrams(s string, p string) (ans []int) {
  m, n := len(s), len(p)
  if m < n {
    return
  }
  cnt1 := [26]int{}
  cnt2 := [26]int{}
  for _, c := range p {
    cnt1[c-'a']++
  }
  for _, c := range s[:n-1] {
    cnt2[c-'a']++
  }
  for i := n - 1; i < m; i++ {
    cnt2[s[i]-'a']++
    if cnt1 == cnt2 {
      ans = append(ans, i-n+1)
    }
    cnt2[s[i-n+1]-'a']--
  }
  return
}
function findAnagrams(s: string, p: string): number[] {
  const m = s.length;
  const n = p.length;
  const ans: number[] = [];
  if (m < n) {
    return ans;
  }
  const cnt1: number[] = new Array(26).fill(0);
  const cnt2: number[] = new Array(26).fill(0);
  const idx = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
  for (const c of p) {
    ++cnt1[idx(c)];
  }
  for (const c of s.slice(0, n - 1)) {
    ++cnt2[idx(c)];
  }
  for (let i = n - 1; i < m; ++i) {
    ++cnt2[idx(s[i])];
    if (cnt1.toString() === cnt2.toString()) {
      ans.push(i - n + 1);
    }
    --cnt2[idx(s[i - n + 1])];
  }
  return ans;
}
impl Solution {
  pub fn find_anagrams(s: String, p: String) -> Vec<i32> {
    let (s, p) = (s.as_bytes(), p.as_bytes());
    let (m, n) = (s.len(), p.len());
    let mut ans = vec![];
    if m < n {
      return ans;
    }

    let mut cnt = [0; 26];
    for i in 0..n {
      cnt[(p[i] - b'a') as usize] += 1;
      cnt[(s[i] - b'a') as usize] -= 1;
    }
    for i in n..m {
      if cnt.iter().all(|&v| v == 0) {
        ans.push((i - n) as i32);
      }
      cnt[(s[i] - b'a') as usize] -= 1;
      cnt[(s[i - n] - b'a') as usize] += 1;
    }
    if cnt.iter().all(|&v| v == 0) {
      ans.push((m - n) as i32);
    }
    ans
  }
}
public class Solution {
  public IList<int> FindAnagrams(string s, string p) {
    int m = s.Length, n = p.Length;
    IList<int> ans = new List<int>();
    if (m < n) {
      return ans;
    }
    int[] cnt1 = new int[26];
    int[] cnt2 = new int[26];
    for (int i = 0; i < n; ++i) {
      ++cnt1[p[i] - 'a'];
    }
    for (int i = 0, j = 0; i < m; ++i) {
      int k = s[i] - 'a';
      ++cnt2[k];
      while (cnt2[k] > cnt1[k]) {
        --cnt2[s[j++] - 'a'];
      }
      if (i - j + 1 == n) {
        ans.Add(j);
      }
    }
    return ans;
  }
}

Solution 2

class Solution:
  def findAnagrams(self, s: str, p: str) -> List[int]:
    m, n = len(s), len(p)
    ans = []
    if m < n:
      return ans
    cnt1 = Counter(p)
    cnt2 = Counter()
    j = 0
    for i, c in enumerate(s):
      cnt2[c] += 1
      while cnt2[c] > cnt1[c]:
        cnt2[s[j]] -= 1
        j += 1
      if i - j + 1 == n:
        ans.append(j)
    return ans
class Solution {
  public List<Integer> findAnagrams(String s, String p) {
    int m = s.length(), n = p.length();
    List<Integer> ans = new ArrayList<>();
    if (m < n) {
      return ans;
    }
    int[] cnt1 = new int[26];
    for (int i = 0; i < n; ++i) {
      ++cnt1[p.charAt(i) - 'a'];
    }
    int[] cnt2 = new int[26];
    for (int i = 0, j = 0; i < m; ++i) {
      int k = s.charAt(i) - 'a';
      ++cnt2[k];
      while (cnt2[k] > cnt1[k]) {
        --cnt2[s.charAt(j++) - 'a'];
      }
      if (i - j + 1 == n) {
        ans.add(j);
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> findAnagrams(string s, string p) {
    int m = s.size(), n = p.size();
    vector<int> ans;
    if (m < n) {
      return ans;
    }
    vector<int> cnt1(26);
    for (char& c : p) {
      ++cnt1[c - 'a'];
    }
    vector<int> cnt2(26);
    for (int i = 0, j = 0; i < m; ++i) {
      int k = s[i] - 'a';
      ++cnt2[k];
      while (cnt2[k] > cnt1[k]) {
        --cnt2[s[j++] - 'a'];
      }
      if (i - j + 1 == n) {
        ans.push_back(j);
      }
    }
    return ans;
  }
};
func findAnagrams(s string, p string) (ans []int) {
  m, n := len(s), len(p)
  if m < n {
    return
  }
  cnt1 := [26]int{}
  cnt2 := [26]int{}
  for _, c := range p {
    cnt1[c-'a']++
  }
  j := 0
  for i, c := range s {
    cnt2[c-'a']++
    for cnt2[c-'a'] > cnt1[c-'a'] {
      cnt2[s[j]-'a']--
      j++
    }
    if i-j+1 == n {
      ans = append(ans, j)
    }
  }
  return
}
function findAnagrams(s: string, p: string): number[] {
  const m = s.length;
  const n = p.length;
  const ans: number[] = [];
  if (m < n) {
    return ans;
  }
  const cnt1: number[] = new Array(26).fill(0);
  const cnt2: number[] = new Array(26).fill(0);
  const idx = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
  for (const c of p) {
    ++cnt1[idx(c)];
  }
  for (let i = 0, j = 0; i < m; ++i) {
    const k = idx(s[i]);
    ++cnt2[k];
    while (cnt2[k] > cnt1[k]) {
      --cnt2[idx(s[j++])];
    }
    if (i - j + 1 === n) {
      ans.push(j);
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文