资讯专栏INFORMATION COLUMN

[LeetCode] 438. Find All Anagrams in a String [滑动窗

muzhuyu / 1194人阅读

Problem

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

Solution dumb version
class Solution {
    public List findAnagrams(String s, String p) {
        List res = new ArrayList<>();
        if (s == null || p == null || s.length() < p.length()) return res;
        for (int i = 0; i <= s.length()-p.length(); i++) {
            if (isAnagram(s.substring(i, i+p.length()), p)) res.add(i);
        }
        return res;
    }
    private boolean isAnagram(String a, String b) {
        int[] dict = new int[256];
        for (char ch: a.toCharArray()) {
            dict[ch]++;
        }
        for (char ch: b.toCharArray()) {
            dict[ch]--;
            if (dict[ch] < 0) return false;
        }
        return true;
    }
}
sliding window
class Solution {
    public List findAnagrams(String s, String p) {
        List res = new LinkedList<>();
        if (s.length() == 0 || p.length() == 0 || s.length() < p.length()) return res;
        
        int[] dict = new int[26];
        for (char ch: p.toCharArray()) {
            dict[ch-"a"]++;
        }
        
        int start = 0, end = p.length(), diff = p.length();
        
        for (int i = 0; i < p.length(); i++) {
            int index = s.charAt(i)-"a";
            dict[index]--;
            if (dict[index] >= 0) diff--;
        }
        if (diff == 0) res.add(0);
        
        while (end < s.length()) {
            int index = s.charAt(start)-"a";
            if (dict[index] >= 0) diff++;
            
            dict[index]++;
            start++;
            
            index = s.charAt(end)-"a";
            dict[index]--;
            if (dict[index] >= 0) diff--;
            end++;
            
            if (diff == 0) res.add(start);
        }
        
        return res;
    }
}
update 2018-10 with comments
class Solution {
    public List findAnagrams(String s, String p) {
        List res = new LinkedList<>();
        if (s.length() == 0 || p.length() == 0 || s.length() < p.length()) return res;
        
        int[] dict = new int[26];
        for (char ch: p.toCharArray()) {
            dict[ch-"a"]++;
        }
        
        int start = 0, end = p.length(), diff = p.length();
        
        for (int i = 0; i < p.length(); i++) {
            int index = s.charAt(i)-"a";
            dict[index]--;
            if (dict[index] >= 0) diff--;
        }
        if (diff == 0) res.add(0);
        
        while (end < s.length()) {
            //will release the s.charAt(start), 
            //so if >= 0 in map, that means we released one char in anagram, so increase diff by 1, 
            //else it"s not in anagram of p, continue
            //after release, increase it back in map
            //shift start
            int index = s.charAt(start)-"a";
            if (dict[index] >= 0) diff++;
            dict[index]++;
            start++;
            
            //will store the s.charAt(end), 
            //so first decrease in map, 
            //if still >= 0, that means we saved one char that is in anagram p, so reduce diff by 1
            //else it"s not in anagram of p, continue
            //shift end
            index = s.charAt(end)-"a";
            dict[index]--;
            if (dict[index] >= 0) diff--;
            end++;
            
            //if diff became 0 at the end of this iteration, add index start to result
            if (diff == 0) res.add(start);
        }
        
        return res;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77382.html

相关文章

  • leetcode438. Find All Anagrams in a String

    摘要:题目要求思路和代码这是一个简单的双指针问题,即左指针指向可以作为起点的子数组下标,右指针则不停向右移动,将更多的元素囊括进来,从而确保该左右指针内的元素是目标数组的一个兄弟子数组即每个字母的个数均相等左指针记录每个字母出现的次数拷贝一个 题目要求 Given a string s and a non-empty string p, find all the start indices ...

    wangbinke 评论0 收藏0
  • [LeetCode]Find All Anagrams in a String

    摘要:解题思路,就是只顺序不同但个数相同的字符串,那我们就可以利用的思想来比较每个字符串中字符出现的个数是否相等。 Find All Anagrams in a StringGiven a string s and a non-empty string p, find all the start indices of ps anagrams in s. Strings consists of...

    niceforbear 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 题攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0
  • [LeetCode] Group Anagram

    Problem Given an array of strings, group anagrams together. Example: Input: [eat, tea, tan, ate, nat, bat], Output: [ [ate,eat,tea], [nat,tan], [bat] ] Note: All inputs will be in lowercase.The ...

    kid143 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<