亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LeetCode] 438. Find All Anagrams in a String [滑動(dòng)窗

muzhuyu / 1277人閱讀

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;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/77382.html

相關(guān)文章

  • leetcode438. Find All Anagrams in a String

    摘要:題目要求思路和代碼這是一個(gè)簡(jiǎn)單的雙指針問題,即左指針指向可以作為起點(diǎn)的子數(shù)組下標(biāo),右指針則不停向右移動(dòng),將更多的元素囊括進(jìn)來(lái),從而確保該左右指針內(nèi)的元素是目標(biāo)數(shù)組的一個(gè)兄弟子數(shù)組即每個(gè)字母的個(gè)數(shù)均相等左指針記錄每個(gè)字母出現(xiàn)的次數(shù)拷貝一個(gè) 題目要求 Given a string s and a non-empty string p, find all the start indices ...

    wangbinke 評(píng)論0 收藏0
  • [LeetCode]Find All Anagrams in a String

    摘要:解題思路,就是只順序不同但個(gè)數(shù)相同的字符串,那我們就可以利用的思想來(lái)比較每個(gè)字符串中字符出現(xiàn)的個(gè)數(shù)是否相等。 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 評(píng)論0 收藏0
  • 前端 | 每天一個(gè) LeetCode

    摘要:在線網(wǎng)站地址我的微信公眾號(hào)完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個(gè)題。這是項(xiàng)目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語(yǔ)言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號(hào): showImg(htt...

    張漢慶 評(píng)論0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月匯總(100 題攻略)

    摘要:月下半旬攻略道題,目前已攻略題。目前簡(jiǎn)單難度攻略已經(jīng)到題,所以后面會(huì)調(diào)整自己,在刷算法與數(shù)據(jù)結(jié)構(gòu)的同時(shí),攻略中等難度的題目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道題,目前已攻略 100 題。 一 目錄 不折騰的前端,和咸魚有什么區(qū)別...

    tain335 評(píng)論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 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<