• 438. Find All Anagrams in a String 找anagram的初始index, 窗口也能用int[] 26


    窗口也可以用

    int[] map = new int[26];

    https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/636988/Sliding-Window-or-HashTable-or-Java-Explained-with-Diagram-Beats-99

    class Solution {
        public List<Integer> findAnagrams(String s, String p) {
            int[] map = new int[26];
            List<Integer> result = new ArrayList<>();
            
            for(int i=0;i<p.length();i++){
                map[p.charAt(i) - 'a']++;
            }
        
            int windowStart = 0;
            int windowEnd = 0;
            while(windowEnd<s.length()){
            // valid anagram
                if(map[s.charAt(windowEnd) - 'a'] > 0){
                    map[s.charAt(windowEnd++) - 'a']--;
                // window size equal to size of P
                    if(windowEnd-windowStart ==  p.length()){                    
                        result.add(windowStart);
                    }
                }
                // window is of size 0
                else if(windowStart == windowEnd){
                    windowStart ++;
                    windowEnd ++;
                }
                // decrease window size
                else{
                    map[s.charAt(windowStart++) - 'a']++;
                }      
            }
            return result;
        }
    }
     
  • 相关阅读:
    kubernetes构架及组件介绍
    二进制部署k8s
    Git
    Redis sentinel
    redis主从复制
    k8s安装
    基于Jenkins实现可腹部回滚的cicd平台
    Redis基础命令和持久化
    构建自动发现的Docker服务架构
    Redis
  • 原文地址:https://www.cnblogs.com/immiao0319/p/15400715.html
Copyright © 2020-2023  润新知