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

Solution

dumb version

class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> res = new ArrayList<>();
        if (s == null || p == null || s.length() < p.length()) return res;
        for (int i = 0; i <= s.length()-p.length(); i++) {
            if (isAnagram(s.substring(i, i+p.length()), p)) res.add(i);
        }
        return res;
    }
    private boolean isAnagram(String a, String b) {
        int[] dict = new int[256];
        for (char ch: a.toCharArray()) {
            dict[ch]++;
        }
        for (char ch: b.toCharArray()) {
            dict[ch]--;
            if (dict[ch] < 0) return false;
        }
        return true;
    }
}

sliding window


class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> res = new LinkedList<>();
        if (s.length() == 0 || p.length() == 0 || s.length() < p.length()) return res;
        
        int[] dict = new int[26];
        for (char ch: p.toCharArray()) {
            dict[ch-'a']++;
        }
        
        int start = 0, end = p.length(), diff = p.length();
        
        for (int i = 0; i < p.length(); i++) {
            int index = s.charAt(i)-'a';
            dict[index]--;
            if (dict[index] >= 0) diff--;
        }
        if (diff == 0) res.add(0);
        
        while (end < s.length()) {
            int index = s.charAt(start)-'a';
            if (dict[index] >= 0) diff++;
            
            dict[index]++;
            start++;
            
            index = s.charAt(end)-'a';
            dict[index]--;
            if (dict[index] >= 0) diff--;
            end++;
            
            if (diff == 0) res.add(start);
        }
        
        return res;
    }
}

update 2018-10 with comments

class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> res = new LinkedList<>();
        if (s.length() == 0 || p.length() == 0 || s.length() < p.length()) return res;
        
        int[] dict = new int[26];
        for (char ch: p.toCharArray()) {
            dict[ch-'a']++;
        }
        
        int start = 0, end = p.length(), diff = p.length();
        
        for (int i = 0; i < p.length(); i++) {
            int index = s.charAt(i)-'a';
            dict[index]--;
            if (dict[index] >= 0) diff--;
        }
        if (diff == 0) res.add(0);
        
        while (end < s.length()) {
            //will release the s.charAt(start), 
            //so if >= 0 in map, that means we released one char in anagram, so increase diff by 1, 
            //else it's not in anagram of p, continue
            //after release, increase it back in map
            //shift start
            int index = s.charAt(start)-'a';
            if (dict[index] >= 0) diff++;
            dict[index]++;
            start++;
            
            //will store the s.charAt(end), 
            //so first decrease in map, 
            //if still >= 0, that means we saved one char that is in anagram p, so reduce diff by 1
            //else it's not in anagram of p, continue
            //shift end
            index = s.charAt(end)-'a';
            dict[index]--;
            if (dict[index] >= 0) diff--;
            end++;
            
            //if diff became 0 at the end of this iteration, add index start to result
            if (diff == 0) res.add(start);
        }
        
        return res;
    }
}

linspiration
161 声望53 粉丝