1. 程式人生 > >【leetcode】438. Find All Anagrams in a String【E】

【leetcode】438. 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 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".
Subscribe to see which companies asked this question 用字典來儲存,判斷 特別二,最開始竟然又用了set,已經不是第一次了,sigh
class Solution(object):
    def findAnagrams(self, s, p):
        
        lp = len(p)
        ls = len(s)
        
        ds = {}
        dp = {}
        
        
        for i in s[:lp]:
            ds[i] = ds.get(i,0) + 1
        for i in p[:lp]:
            dp[i] = dp.get(i,0) + 1
            
        
        res = []
        i = 0
        
        while i < (ls-lp):
            if dp == ds:
                res += i,
            
            ds[s[i]] -= 1
            if ds[s[i]] == 0:
                del ds[s[i]]
            ds[s[i+lp]] = ds.get(s[i+lp],0) + 1
            i+= 1
        if ds == dp:
            res += i,
            
        return res
        """
        :type s: str
        :type p: str
        :rtype: List[int]
        """


相關推薦

leetcode438. Find All Anagrams in a StringE

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

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

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

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

python leetcode 438. Find All Anagrams in a String

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

[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為這

[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 解題報告

題目: 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

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

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

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演算法題-Find All Anagrams in a String(Java實現)

這是悅樂書的第228次更新,第240篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第95題(順位題號是438)。給定一個字串s和一個非空字串p,找到s中p的字謎的所有起始索引。字串僅由小寫英文字母組成,字串s和p的長度不會大於20,100。輸出順序無關緊要。例如: 輸入

438. Find All Anagrams in a String 找到字符串中所有字母異位詞

app while pan temp 左移 pen 語句 port code 1. 原始題目 給定一個字符串 s 和一個非空字符串 p,找到 s 中所有是 p 的字母異位詞的子串,返回這些子串的起始索引。 字符串只包含小寫英文字母,並且字符串 s 和 p 的長度都不超過

leetcodeFind All Anagrams in a String438

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