一、题目
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.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-all-anagrams-in-a-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
二、 思路
套模板,debug的地方就四个或者五个,弄明白每个关键点到底应该怎么写就ok了。
二、自己写的
1.代码
第一次写
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<Integer>();
HashMap<Character,Integer> need = new HashMap<Character,Integer>();
HashMap<Character,Integer> window = new HashMap<Character,Integer>();
int left = 0, right = 0;
int valid = 0;
int len = Integer.MAX_VALUE;
for (char c : p.toCharArray()) {
need.put(c, need.getOrDefault(c, 0) + 1);
}
//第一个debug点,考虑什么时候窗口右移
while (right < s.length()) {
char c = s.charAt(right);
right++;
len = right - left;
//第二个debug点,什么时候window和valid“变大”
if (need.containsKey(c)) {
window.put(c, window.getOrDefault(c, 0) + 1);
if (need.get(c).equals(window.get(c)))
valid++;
}
//第三个debug点,什么时候开始窗口左移
while (len >= p.length()) {
//什么情况下满足要求
if ((len == p.length()) && (valid == need.size()))
res.add(left);
char d = s.charAt(left);
left++;
len = right - left;
//第四个debug点,什么时候window和valid“变小”
if (need.containsKey(d)) {
if (need.get(d).equals(window.get(d)))
valid--;
window.put(d, window.get(d) - 1);
}
}
}
return res;
}
}
//正确,弄清楚那几个点的逻辑就ok。
三、标准答案
1.方法一(滑动窗口)
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<Integer>();
HashMap<Character,Integer> need = new HashMap<Character,Integer>();
HashMap<Character,Integer> window = new HashMap<Character,Integer>();
int left = 0, right = 0;
int valid = 0;
int len = Integer.MAX_VALUE;
for (char c : p.toCharArray()) {
need.put(c, need.getOrDefault(c, 0) + 1);
}
//第一个debug点,考虑什么时候窗口右移
while (right < s.length()) {
char c = s.charAt(right);
right++;
len = right - left;
//第二个debug点,什么时候window和valid“变大”
if (need.containsKey(c)) {
window.put(c, window.getOrDefault(c, 0) + 1);
if (need.get(c).equals(window.get(c)))
valid++;
}
//第三个debug点,什么时候开始窗口左移
while (len >= p.length()) {
//什么情况下满足要求
if ((len == p.length()) && (valid == need.size()))
res.add(left);
char d = s.charAt(left);
left++;
len = right - left;
//第四个debug点,什么时候window和valid“变小”
if (need.containsKey(d)) {
if (need.get(d).equals(window.get(d)))
valid--;
window.put(d, window.get(d) - 1);
}
}
}
return res;
}
}
2.总结
弄明白那几个debug点的逻辑就ok了。