Substring Anagrams

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 40,000.

The order of output does not matter.

Example

Given s = "cbaebabacd" p = "abc"

return [0, 6]

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

思路: HashTableRelatedProblems : Anagrams  Two Strings Are Anagrams


public class Solution {
    /**
     * @param s a string
     * @param p a non-empty string
     * @return a list of index
     */
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> result = new ArrayList<>();
        if (s.length() < p.length()) {
            return result;
        }
        int[] numS = new int[256];
        int[] numP = new int[256];
        for (int i = 0; i < p.length(); i++) {
            numS[s.charAt(i) - ‘a‘]++;
            numP[p.charAt(i) - ‘a‘]++;
        }
        if (Arrays.equals(numS,numP)) {
            result.add(0);
        }
        for (int i = p.length(); i < s.length(); i++) {
            numS[s.charAt(i) - ‘a‘]++;
            numS[s.charAt(i - p.length()) - ‘a‘]--;
            if (Arrays.equals(numS,numP)) {
                result.add(i - p.length() + 1);
            }
        }
        return result;
    }
}
时间: 2024-10-01 05:02:51

Substring Anagrams的相关文章

[Lintcode] Substring Anagrams

Substring Anagrams 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 40,000. The order of outp

leetcode_438_Find All Anagrams in a String_哈希表_java实现

题目: 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 mat

[LeetCode] 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.

438. Find All Anagrams in a String

Problem statement 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 outpu

Find All Anagrams in a String Leetcode

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.

Leetcode 438. Find All Anagrams in s 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.

Leetcode——438. Find All Anagrams in a String【java】

第一反应感觉是排列组合及字符匹配问题 [既不是可重复排列,有可能不是全排列] 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,10

Leetcode: 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.

[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.Ex