zoukankan      html  css  js  c++  java
  • 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
     
  • 相关阅读:
    HUAWEI防火墙双出口据链路带宽负载分担
    HUAWEI防火墙双出口根据链路优先级主备备份
    HUAWEI防火墙双出口环境下私网用户通过NAPT访问Internet
    如何实现IP话机接入交换机?
    WLAN-AC+AP,动态负载均衡用户量,避免某一个AP负载过重
    WLAN-AC+AP射频一劳永逸的调优方式
    中大型企业有线无线用户统一接入(实施笔记)
    js获取当前时间,返回日期yyyy-MM-dd
    cookie和token都存在在请求头header中,有什么区别,为什么建议使用token?
    vue中cookie的使用——将cookie放在请求头header中
  • 原文地址:https://www.cnblogs.com/immiao0319/p/13064168.html
Copyright © 2011-2022 走看看