成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode]Find All Anagrams in a String

niceforbear / 3081人閱讀

摘要:解題思路,就是只順序不同但個數(shù)相同的字符串,那我們就可以利用的思想來比較每個字符串中字符出現(xiàn)的個數(shù)是否相等。

Find All Anagrams in a String
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".

1.解題思路

anagrams,就是只順序不同但個數(shù)相同的字符串,那我們就可以利用hashtable的思想來比較每個字符串中字符出現(xiàn)的個數(shù)是否相等。
對于兩個字符串我們分別準(zhǔn)備數(shù)組(大小為256)來存儲每個字符出現(xiàn)的次數(shù):
1) 對于p,我們遍歷,并在hp中記錄字符出現(xiàn)的次數(shù);
2) 之后遍歷s,先把當(dāng)前字符的個數(shù)+1,但是需要考慮當(dāng)前index是否已經(jīng)超過了p的長度,如果超過,則表示前面的字符已經(jīng)不予考慮,所以要將index-plen的字符的個數(shù)-1;最后判斷兩個數(shù)組是否相等,如果相等,返回index-plen+1,即為開始的下標(biāo)。

2.代碼

public class Solution {
    public List findAnagrams(String s, String p) {
        List res=new ArrayList();
        if(s.length()==0||s==null||p.length()==0||p==null) return res;
        int[] hs=new int[256];
        int[] hp=new int[256];
        int plen=p.length();
        for(int i=0;i=plen){
                hs[s.charAt(j-plen)]--;
            }
            if(Arrays.equals(hs,hp))
                res.add(j-plen+1);
        }
        return res;
    }
}

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

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/69799.html

相關(guān)文章

  • leetcode438. Find All Anagrams in a String

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

    wangbinke 評論0 收藏0
  • [LeetCode] 438. Find All Anagrams in a String [滑動窗

    Problem Given a string s and a non-empty string p, find all the start indices of ps anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be...

    muzhuyu 評論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
  • leetcode49 Group Anagrams

    摘要:同時使用方法將數(shù)組轉(zhuǎn)化為并利用的直接比較兩個字符串是否相等。通過這種方法效率值提高了不少。 題目要求 Given an array of strings, group anagrams together. For example, given: [eat, tea, tan, ate, nat, bat], Return: [ [ate, eat,tea], [nat,t...

    sunsmell 評論0 收藏0
  • 前端 | 每天一個 LeetCode

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

    張漢慶 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<