438. Find All Anagrams in a String

Description

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

Two-pointer

class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> anagrams = new ArrayList<>();
        if (s == null || p == null || s.isEmpty() || p.isEmpty() || s.length() < p.length()) {
            return anagrams;
        }
        
        int[] need = new int[256];
        
        for (char c : p.toCharArray()) {
            ++need[c];
        }
        
        int left = 0;
        int right = 0;
        int stillNeed = p.length();
        
        while (right < s.length()) {
            // move right everytime, if the character exists in p's hash, decrease the count
            // current hash value >= 1 means the character is existing in p
            if (need[s.charAt(right)] > 0) {
                --stillNeed;
            }
            --need[s.charAt(right)];
            ++right;
            
            // when the count is down to 0, means we found the right anagram
            // then add window's left to result list
            if (stillNeed == 0) {
                anagrams.add(left);
            }
            
            // if we find the window's size equals to p, then we have to 
            // move left (narrow the window) to find the new match window
            // ++ to reset the hash because we kicked out the left
            // only increase the count if the character is in p
            // the count >= 0 indicate it was original in the hash, cuz it won't go below 0
            if (right - left == p.length()) {
                if (need[s.charAt(left)] >= 0) {
                    ++stillNeed;
                }
                
                ++need[s.charAt(left)];
                ++left;
            }
        }
        
        return anagrams;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末嗤瞎,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子听系,更是在濱河造成了極大的恐慌贝奇,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件靠胜,死亡現(xiàn)場離奇詭異掉瞳,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)浪漠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門陕习,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人址愿,你說我怎么就攤上這事该镣。” “怎么了响谓?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵拌牲,是天一觀的道長。 經(jīng)常有香客問我歌粥,道長,這世上最難降的妖魔是什么拍埠? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任失驶,我火速辦了婚禮,結(jié)果婚禮上枣购,老公的妹妹穿的比我還像新娘嬉探。我一直安慰自己,他們只是感情好棉圈,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布涩堤。 她就那樣靜靜地躺著,像睡著了一般分瘾。 火紅的嫁衣襯著肌膚如雪胎围。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天德召,我揣著相機(jī)與錄音白魂,去河邊找鬼。 笑死上岗,一個胖子當(dāng)著我的面吹牛福荸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播肴掷,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼敬锐,長吁一口氣:“原來是場噩夢啊……” “哼背传!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起台夺,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤径玖,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后谒养,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體挺狰,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年买窟,在試婚紗的時候發(fā)現(xiàn)自己被綠了丰泊。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡始绍,死狀恐怖瞳购,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情亏推,我是刑警寧澤学赛,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站吞杭,受9級特大地震影響盏浇,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜芽狗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一绢掰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧童擎,春花似錦滴劲、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至芯砸,卻和暖如春萧芙,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背假丧。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工末购, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人虎谢。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓盟榴,卻偏偏與公主長得像,于是被迫代替她去往敵國和親婴噩。 傳聞我的和親對象是個殘疾皇子擎场,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容