1. 程式人生 > >[python]leetcode(438). Find All Anagrams in a String

[python]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 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.

分析

我的想法是:
把p中的字母存在雜湊表中,然後對s中的子串也建立一個雜湊表,每次掃描一個字元就加入並計數,
如果掃描結束後沒有超出對應的數量,也沒有出現不在p中的字元,那麼就匹配成功。

如果上一次成功,則只需上一個字元和p長度最後一個字元是否相等,若相等則匹配成功,否則轉到下兩種情況。

如果出現不在這個p中的字元,則遍歷直接跳到這個字元之後。

如果某個字元數量超限,則從頭找到等於這個字元的位置跳過,開始下一次匹配。

改進

針對上面的演算法有兩個改進點:

  1. 使用兩個索引(begin, end),使用滑動視窗的形式遍歷,主要是end可以不動,
  2. 使用陣列形式的雜湊表(可以使用defaultdict(int)),操作更加方便,可以把所有值都賦成零,對不在子串中的字元也-1,這樣在使用begin遍歷時,可以分辨出它是否在匹配串中。

理解:
end對每個路過的字元-1,begin對每個字元+1,這樣begin和end中間的字元資訊就記錄在字典中了,字典中的值表示當前子串還需要幾個對應的字元(負數表示不需要)和p匹配。

同時用count記錄當前串是否完成匹配,count主要是記錄字典的統計資訊的,這樣就不用去遍歷字典檢查資訊了。

class Solution(object):
    def findAnagrams(self, s, p):
        from collections import defaultdict
        begin, end = 0, 0
        count = len(p)
        ans = []
        d = defaultdict(int)
        for i in p:
            d[i] += 1

        while
end < len(s): if d[s[end]] > 0: count -= 1 d[s[end]] -= 1 end += 1 #匹配成功 if count == 0: ans.append(begin) #字串長度和p相等,begin向前移動 if end - begin == len(p): #begin向前移動 d[s[begin]] += 1 begin += 1 #加1後>=1,說明子串還需要begin對應的字元,即begin拋棄的字元還有用我們需要在後面補上。 if d[s[begin-1]] >= 1: count += 1 return ans

總結

使用count記錄完成匹配還需要幾個字元,使用d(hash map)記錄begin和end之間的子串的資訊,利用這樣的資料結構還可以解決更多的子串匹配問題,請參考

相關推薦

python leetcode 438. Find All Anagrams in a String

可以用字典做 也可以用再加個函式判斷是否是Anagrams class Solution: def findAnagrams(self, s, p): """ :type s: str :type p: str :r

[python]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 p’s anagrams in s. Strings consists of

LeetCode--438. Find All Anagrams in a String(找到所有字元組合)Python

題目: 給定兩個字串s、p。找到s中和p包含相同字元的所有子串。 解題思路: 直接暴力搜尋s,固定長度,對p和s固定長度的子串分別進行排序,看兩者排序後是否相同。這樣是一種方法,但會超時。 另外一種方法是使用雜湊的方法。用兩個個1*26的list分別儲存p和s子串中各個字元

[leetcode-438-Find All Anagrams in a String]

not plan english urn tco bst ice style ons Given a string s and a non-empty string p, find all the start indices of p‘s anagrams in s.Str

leetcode-438-Find All Anagrams in a String

IT -- 新的 iss HA nag cpp mat strings 題目描述: Given a string s and a non-empty string p, find all the start indices of p‘s anagrams in s. Str

[LeetCode] 438. 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

leetcode 438. Find All Anagrams in a String

temp class int ram while lee all () 正在 題意:在s串中找到與p串所用字符種類,數目相同的所有子串的起始位置。初始化兩個變量start,end,diff。start,end為s串中目前正在與p串比較的子串的起始位置和終止位置。diff為這

[Leetcode] 438. 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

Leetcode 438. Find All Anagrams in a String (Easy) (cpp)

Leetcode 438. Find All Anagrams in a String (Easy) (cpp) Tag: Hash Table Difficulty: Easy /* 43

LeetCode 438. 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

[leetcode]438. Find All Anagrams in a String

題目連結:https://leetcode.com/problems/find-all-anagrams-in-a-string/ Given a string s and a non-empty string p, find all the start indices

LeetCode438. Find All Anagrams in a String 解題報告(Python

目錄題目描述題目大意解題方法滑動視窗雙指標日期 題目描述 Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s. Strings

leetcodeFind All Anagrams in a String438

題目: 給定一個字串 s 和一個非空字串 p,找到 s 中所有是 p 的字母異位詞的子串,返回這些子串的起始索引。 字串只包含小寫英文字母,並且字串 s 和 p 的長度都不超過 20

LeetCode438. 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 l

[leetcode]438. Find All Anagrams in a <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c695b2b4afa8a1868ca7b0a7">[email p

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 o

leetcode438. Find All Anagrams in a String【E】

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 let

【easy】438.Find All Anagrams in a String 找出字符串中所有的變位詞

真的是 for 所有 find 等於 哈希 bstr empty amp Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start index = 0 is "ab"

438 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

438. 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 lette

leetcode No438. Find All Anagrams in a String

Question: Given a string s and a non-empty string p, find all the start indices of p's anagrams in