一、题目
给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引。
字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100。
说明:
- 字母异位词指字母相同,但排列不同的字符串。
- 不考虑答案输出的顺序。
示例 1:
输入:
s: "cbaebabacd" p: "abc"
输出:
[0, 6]
解释:
起始索引等于 0 的子串是 "cba", 它是 "abc" 的字母异位词。
起始索引等于 6 的子串是 "bac", 它是 "abc" 的字母异位词。
示例 2:
输入:
s: "abab" p: "ab"
输出:
[0, 1, 2]
解释:
起始索引等于 0 的子串是 "ab", 它是 "ab" 的字母异位词。
起始索引等于 1 的子串是 "ba", 它是 "ab" 的字母异位词。
起始索引等于 2 的子串是 "ab", 它是 "ab" 的字母异位词。
二、解决
1、暴力破解
思路:
移动窗口,取每次窗口的字符串排序与p比较。
代码: 略。
时间复杂度: O(nklogk), k为p长度。
空间复杂度: O(k)
2、窗口哈希
思路:
Step1:统计p中各个字符的个数。
Step2:对s移动窗口,取窗口字符串,统计字符是否与p中字符个数一一相同。
Step3:本方法具体实现,若相同,这里对应hash数组对应字符统计个数正好与p各个字符统计个数相同,然后相减为0,移动窗口长度也正好为p的长度。将左边位置记录下来,待遍历完毕后,结束循环。
代码:
class Solution {
public List<Integer> findAnagrams(String s, String p) {
int[] hash = new int[26];
int left = 0, right = 0; //[l,r) is window
List<Integer> res = new ArrayList<Integer>();
for (int i = 0; i < p.length(); i++) {
hash[p.charAt(i) - 'a']++;
}
while (right < s.length()){
//if-else guarantee the character in window must be useful.
if (hash[s.charAt(right) - 'a'] > 0){
// expand window
// 字符存在,对应位置[hash]数量-1 or 不存在的字符-1,复原
hash[s.charAt(right) - 'a']--;
right++;
}
else{
// shrunk window
// 字符存在,对应位置[hash]数量恢复 or 字符不存在,对应数量+1
hash[s.charAt(left) - 'a']++;
left++;
}
if (right-left == p.length()){
res.add(left);
}
}
return res;
}
}
时间复杂度: O(n)
空间复杂度: O(1)
测试程序:
上述程序还是有几个关键步骤需要理解的,最好自己多多Debug,加深理解。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
//Java:找到字符串中所有字母异位词
public class P438FindAllAnagramsInAString{
static class Solution {
public List<Integer> findAnagrams(String s, String p) {
int[] hash = new int[26];
int l = 0, r = 0; //[l,r) is window
List<Integer> res = new ArrayList<Integer>();
for (int i = 0; i < p.length(); i++) {
hash[p.charAt(i) - 'a']++;
}
while (r < s.length()){
//if-else guarantee the character in window must be useful.
if (hash[s.charAt(r) - 'a'] > 0){
//expand window
// 字符存在,对应位置[hash]数量-1 or 不存在的字符-1,复原
hash[s.charAt(r) - 'a']--;
r++;
}
else{
//shrunk window
// 字符存在,相应位置数量恢复 or 字符不存在,对应数量+1
hash[s.charAt(l) - 'a']++;
l++;
}
if(l<=r && r<s.length()) {
System.out.println("l:"+l+" r:"+r+" substr: "+s.substring(l,r));
}
if (r-l == p.length()){
res.add(l);
}
}
return res;
}
}
public static class MainClass {
public static String stringToString(String input) {
if (input == null) {
return "null";
}
return input;
}
public static String integerArrayListToString(List<Integer> nums, int length) {
if (length == 0) {
return "[]";
}
String result = "";
for(int index = 0; index < length; index++) {
Integer number = nums.get(index);
result += Integer.toString(number) + ", ";
}
return "[" + result.substring(0, result.length() - 2) + "]";
}
public static String integerArrayListToString(List<Integer> nums) {
return integerArrayListToString(nums, nums.size());
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
String s = stringToString(line);
line = in.readLine();
String p = stringToString(line);
List<Integer> ret = new Solution().findAnagrams(s, p);
String out = integerArrayListToString(ret);
System.out.print(out);
}
}
}
}
测试案例:
# Input
cbeaabc
abc
# Output
[4]
三、参考
1、Shortest/Concise JAVA O(n) Sliding Window Solution
2、17ms Java sliding window
3、Sliding Window | HashTable | Java Explained with Diagram , Beats 99%
4、Sliding Window algorithm template to solve all the Leetcode substring search problem.
5、[LeetCode] 438. 找到字符串中所有字母异位词