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 442. Find All Duplicates in an ArrayLeetCode
- LeetCode 394. Decode String All In OneLeetCode
- LeetCode 448. Find All Numbers Disappeared in an ArrayLeetCodeAPP
- Leetcode 49 Group AnagramsLeetCode
- LeetCode 49. Group AnagramsLeetCode
- [LeetCode] 1545. Find Kth Bit in Nth Binary StringLeetCode
- LeetCode 1209. Remove All Adjacent Duplicates in String II 有坑LeetCodeREM
- Find All Numbers Disappeared in an ArrayAPP
- string的find()與npos
- [LeetCode] Find the Duplicate NumberLeetCode
- [LeetCode] Rotate StringLeetCode
- JavaScript string charCodeAt() vs codePointAt() All In OneJavaScript
- LeetCode Patching Array All In OneLeetCode
- std::string的find問題研究
- [LeetCode] 277. Find the CelebrityLeetCode
- LeetCode 389. Find the DifferenceLeetCode
- JavaScript object array sort by string bug All In OneJavaScriptObject
- Leetcode 481 Magical StringLeetCode
- Leetcode 30 Substring with Concatenation of All WordsLeetCode
- [LeetCode] 2028. Find Missing ObservationsLeetCode
- [leetcode] 890. Find and Replace PatternLeetCode
- LeetCode之Find Common Characters(Kotlin)LeetCodeKotlin
- [LeetCode] 724. Find Pivot IndexLeetCodeIndex
- 【Leetcode】767. Reorganize StringLeetCode
- leetcode 344. Reverse StringLeetCode
- 【Leetcode】1528. Shuffle StringLeetCode
- Leetcode 8 String to Integer (atoi)LeetCode
- Leetcode 151 Reverse Words in a StringLeetCode
- string型別資料的find函式型別函式
- LeetCode 735. Asteroid Collision All In OneLeetCodeAST
- LeetCode Greatest Common Divisor of Strings All In OneLeetCode
- [LeetCode] 3238. Find the Number of Winning PlayersLeetCode
- [LeetCode] 3011. Find if Array Can Be SortedLeetCode
- [LeetCode] 2831. Find the Longest Equal SubarrayLeetCode
- [leetcode] 1394. Find Lucky Integer in an ArrayLeetCode
- 【Leetcode】1673. Find the Most Competitive SubsequenceLeetCode
- [LeetCode] Find First and Last Position of Element in SortedLeetCodeAST