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 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".
給出兩個字串s和p找出s中所有和p組成元素相同的子串起始下標
一開始用了暴力法,一個一個比較,果不其然超時了。
然後網上查了下,發現是用hash來做的。看了一下相關程式碼。豁然開朗,時間複雜度瞬間降為o(n)
char[256] hash來記錄p中的組成
left記錄視窗左端的index
right記錄視窗右端的index
count記錄剩餘未匹配的元素個數 這個count是時間複雜段降低的關鍵所在
public List<Integer> findAnagrams(String s, String p) {
List<Integer> list = new ArrayList<>();
if(s.length() == 0 || p.length() ==0) return list;
if(s.length() < p.length()) return list;
int[] hash = new int[256];
char[] pch = p.toCharArray();
char[] sch = s.toCharArray();
//記錄還未匹配的個數
int count = 0;
//將p對映到hash表中
for (char ch : pch) {
hash[ch]++;
count++;
}
//視窗左右的index初始化
int left = 0;
int right = 0;
while (right < sch.length) {
//
if(hash[sch[right]]-- > 0){
count--;
}
if (count == 0) {
list.add(left);
}
if (right - left + 1!= p.length()) {
right++;
} else {
if(hash[sch[left]]++ >= 0) count++;
right++;
left++;
}
}
return list;
}
雖然是個簡單題,但難度和含金量不亞於中等題。
這個題讓我對hash對映的理解更加深刻了,好題,贊一個!
相關文章
- (轉)leetcode:Find All Anagrams in a String 滑動視窗方法總結LeetCode
- Leetcode AnagramsLeetCode
- Leetcode 442. Find All Duplicates in an ArrayLeetCode
- Leetcode-AnagramsLeetCode
- Anagrams leetcode javaLeetCodeJava
- LeetCode 448. Find All Numbers Disappeared in an ArrayLeetCodeAPP
- Leetcode 49 Group AnagramsLeetCode
- LeetCode 394. Decode String All In OneLeetCode
- LeetCode 49. Group AnagramsLeetCode
- Find All Numbers Disappeared in an ArrayAPP
- [LeetCode] 1545. Find Kth Bit in Nth Binary StringLeetCode
- string的find()與npos
- 448. Find All Numbers Disappeared in an ArrayAPP
- LeetCode 1209. Remove All Adjacent Duplicates in String II 有坑LeetCodeREM
- [LeetCode] Find the Duplicate NumberLeetCode
- LeetCode-Find the CelebrityLeetCode
- Find Peak element leetcodeLeetCode
- JavaScript object array sort by string bug All In OneJavaScriptObject
- LeetCode 389. Find the DifferenceLeetCode
- LeetCode-Find the Duplicate NumberLeetCode
- Leetcode-Find Peak ElementLeetCode
- [LeetCode] Rotate StringLeetCode
- [leetcode] Scramble StringLeetCode
- string型別資料的find函式型別函式
- LeetCode Patching Array All In OneLeetCode
- [LeetCode] 277. Find the CelebrityLeetCode
- [LeetCode] 724. Find Pivot IndexLeetCodeIndex
- [leetcode] 890. Find and Replace PatternLeetCode
- leetcode287: Find the Duplicate NumberLeetCode
- JavaScript string charCodeAt() vs codePointAt() All In OneJavaScript
- Leetcode 481 Magical StringLeetCode
- Leetcode Reverse Words in a StringLeetCode
- leetcode Reverse Words in a StringLeetCode
- leetcode String to Integer (atoi)LeetCode
- Leetcode-Scramble StringLeetCode
- Leetcode - String to Integer (atoi)LeetCode
- Scramble String leetcode javaLeetCodeJava
- [LeetCode] Find First and Last Position of Element in SortedLeetCodeAST