問題:
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,找到p的重組字在s中出現的開始位置。
字符串全部由小寫字母組成,s和p的長度都不超過20100。
輸出的順序無所謂。
例1:輸入:
s: "cbaebabacd" p: "abc"
輸出:
[0, 6]
解釋:
“abc”的重組字“cba”可以從0開始找到。
“abc”的重組字“bac”可以從6開始找到。例2:
輸入:
s: "abab" p: "ab"
輸出:
[0, 1, 2]
解釋:
“ab”的重組字“ab”可以從0開始找到。
“ab”的重組字“ba”可以從1開始找到。
“ab”的重組字“ab”可以從2開始找到。
思路:
這道題的意思就是給兩個字符串,看p的順序打亂后的所有可能的字符串在s中能不能找到,找得到就把所有找到的開始的位置記錄下來。這個大概的思路要用到兩個標記,去一點點比對p的重組字有沒有可能找到,找不找得到這一點,不可能把p的所有可能的重組字先列出來,就只能一個字母一個字母地判斷,如果用過了就去掉,看是全部字母都能找到還是只能找到部分。注意題目說了只有小寫字母,而且p的長度不為空。我自己的做法在超長的測試用例時超時了,用的循環太多了。這里看別人非常精簡巧妙的一個方法。
他山之石:
public List<Integer> findAnagrams(String s, String p) {
List<Integer> list = new ArrayList<>();
if (s == null || s.length() == 0 || p == null || p.length() == 0) return list;
int[] hash = new int[256];
for (char c : p.toCharArray()) {
hash[c]++;
}
int left = 0, right = 0, count = p.length();
while (right < s.length()) {
if (hash[s.charAt(right++)]-- >= 1) count--;
if (count == 0) list.add(left);
if (right - left == p.length() && hash[s.charAt(left++)]++ >= 0) count++;
}
return list;
}
這個代碼非常精簡,頻繁地用到了后置++和--,這里要注意的是后置的計算是會先做玩判斷后再進行加和減的,還有就是&&這個判斷符,只有當前面的條件判斷成功了才會去進行后面的判斷,也就是說如果前面的不成立,后面的判斷根本不會執行,這里也巧妙地利用了這個特性。這個代碼可能過于精簡了,來看一下稍微復雜化一點的同樣的代碼。
public class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> list = new ArrayList<>();
if (s == null || s.length() == 0 || p == null || p.length() == 0) return list;
int[] hash = new int[256];
for (char c : p.toCharArray()) {
hash[c]++;
}
int left = 0, right = 0, count = p.length();
while (right < s.length()) {
if (hash[s.charAt(right)] >= 1) {
count--;
}
hash[s.charAt(right)]--;
right++;
if (count == 0) {
list.add(left);
}
if (right - left == p.length() ) {
if (hash[s.charAt(left)] >= 0) {
count++;
}
hash[s.charAt(left)]++;
left++;
}
}
return list;
}
}
這個算法首先是判斷為空的情況,然后創建了一個數組用來存儲p中的各個字符的數量,這是對于判斷有無字母的一個很好的辦法,先用每個字母位置的數量來表示各個字母的數量,接下來每次對各個字母的數量進行加減就可以了,這里的數組名hash只是一個數組,不要和哈希算法弄混了。
創建了左右兩個標志位,一個用來表示判斷字符串的起始位,一個表示終止位,都從0開始,還一個變量表示p的長度。
只要右標志位沒有到s的最右邊,就進行大循環。
對右標志位記錄的s中的字母進行判斷,看p中有沒有,這里就是用那個表示p中字母數量的數組來進行判斷的,找到了,就把表示要判斷的字符串長度減一,不管有沒有找到,都要把數量數組減少,右標志位右移,這是為了之后進行判斷,因為我們要找的的字符串始終處于左和右的標志位的中間。
如果要找的字符串的長度減少到0了,說明我們在左右標志位中間找到了p字符串長度的重組字,這時候就可以把左標志位,也就是開始的位置,添加到結果數組中。
在循環的最后,先判斷左右標志位中間是否是p的長度,是的話,我們就該把左標志位也右移了,而右移之前,先要看看左標志位這個數我們是否找到過,找到過則要把count數量補回1,不論有沒有找到過,都要講數組中的對應的字母數量補回1。
整個過程感覺理解的也不是很到位,希望大家指點一下。
合集:https://github.com/Cloudox/LeetCode-Record