1. 程式人生 > >438. Find All Anagrams in a String

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

滑動視窗,每次新增右邊的一個字元,刪除左邊的一個字元,看看是不是滿足各種字元數都大於等於p中各種字元的字元數的條件

注意檢測的時候完全沒必要掃描窗口裡的所有字元來統計個數,視窗滑動時只需要考慮一個新增進來的字元和一個刪除的字元,因為只有這兩個字元出現了變動,用needed標記“缺失”的字元數,假設字元為X,串P中字元X的個數為P(X),當X等於P(X)且X需要刪除的時候才會使得“缺失”的字元數+1 ;當X等於P(X)-1且需要新增的時候才會使得“缺失”的字元數-1

public class Solution {
    public List<Integer> findAnagrams(String s, String p)
	{
		List<Integer> retlist=new ArrayList<>();
		int slen=s.length();
		int plen=p.length();
		if(slen<plen)
			return retlist;
		int[] carr=new int[128];
		for(int i=0;i<plen;i++)
			carr[p.charAt(i)]++;
		
		int need=0;
		int[] count=new int[128];
		
		for(int i=0;i<plen;i++)
			count[s.charAt(i)]++;
		
		for(char c='a';c<='z';c++)
			if(count[c]<carr[c])
				need++;
		
		if(need==0)
			retlist.add(0);
		
		for(int i=plen;i<slen;i++)
		{
			char preChar=s.charAt(i-plen);
			char nowChar=s.charAt(i);
			if(--count[preChar]==carr[preChar]-1)
				need++;
			if(++count[nowChar]==carr[nowChar])
				need--;
			if(need==0)
				retlist.add(i-plen+1);
		}
		
		return retlist;
	}
}


相關推薦

[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

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

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 l

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

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

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

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

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 找到字符串中所有字母異位詞

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

leetcode之Find All Anagrams in a String438

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

子串字謎 · Find All Anagrams in a String

程序 code 所有 lex 空間 關於 gpo 異常 復雜 [抄題]: 給定一個字符串 s 和一個 非空字符串 p ,找到在 s 中所有關於 p 的字謎的起始索引。字符串僅由小寫英文字母組成,字符串 s 和 p 的長度不得大於 40,000。輸出順序無關緊要. 樣例 給