码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-06-08 11:06:25      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:att   ice   length   lap   none   spl   output   开头   counter   

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

 

 

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

标签:att   ice   length   lap   none   spl   output   开头   counter   

原文地址:https://www.cnblogs.com/immiao0319/p/13064168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!