438. Find All Anagrams in a String 438.查找字符串中的所有Anagrams

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.

Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.

The order of output does not matter.

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".

结果有模板。几个大的步骤:end先移动 begin后移动
 while(end < s.length()){
            从end开始减去s
            end++;//把map里的t在s里找一遍,移动end直到t结束
            
            while(counter == 0){//找完了t,map已经空了的时候
                从begin开头存一个s
                存到结果集合
                begin++;//begin往前一位
            }
            
        }

test case:

"abab"
"ab"
stdout

counter = 2

end = 0
结尾处的字母c = a
counter--之后的mapsize = 1
end++之后 = 1
-------
end = 1
结尾处的字母c = b
counter--之后的mapsize = 0
end++之后 = 2
-------
begin = 0
开头处的字母tempc = a //这个key还是有的
counter++之后的mapsize = 1 //就加了一个值
此时的begin = 0
 
end = 2
结尾处的字母c = a
counter--之后的mapsize = 0 //这里还要减,所以下面加1
end++之后 = 3 //又找到了
-------
begin = 1
开头处的字母tempc = b
counter++之后的mapsize = 1
此时的begin = 1
 
end = 3
结尾处的字母c = b
counter--之后的mapsize = 0
end++之后 = 4
-------
begin = 2
开头处的字母tempc = a
counter++之后的mapsize = 1
此时的begin = 2

counter形容的是0个值的key的个数
class Solution {
    public List<Integer> findAnagrams(String s, String t) {
        List<Integer> result = new ArrayList<Integer>();
        HashMap<Character, Integer> map = new HashMap<>(); 
            
        //cc
        if (s.length() < t.length())
            return result;
            
        int begin, end = 0;
        
        //存t
        for (int i = 0; i < t.length(); i++) {
            char tChar = s.charAt(i);
            map.put(tChar, map.getOrDefault(tChar, 0) + 1);
        }
        int counter = map.size();
        
        //从s结尾开始减,从s开头开始存
        while (end < s.length()) {
            char cEnd = s.charAt(end);
            
            if (map.containsKey(cEnd)) {
                map.put(cEnd, map.get(cEnd) - 1);
                if (map.get(cEnd) == 0)
                    counter--;
            }
            end++;
            
            //t里的都数完了
            while (counter == 0) {
                char cBegin = s.charAt(begin);
                if (map.containsKey(cBegin)) {
                    map.put(cBegin, map.get(cBegin) + 1);
                if (map.get(cBegin) > 0)
                    counter++;
                }
                    
                if (end - begin == t.length())
                    result.add(begin);
                    
                begin++;
            }
        }
        
        return result;
    }
}
View Code

 

 
posted @ 2020-06-08 10:30  苗妙苗  阅读(145)  评论(0编辑  收藏  举报