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.

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

这道题有个很巧妙的移动窗口法,count表示当前left开始的第二个stirng长度的字符串中,有几个不是存在于第二个string里面的,初始值为第二个string的长度。这道题需要回顾一下。

public class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> res = new ArrayList<>();
        if (s == null || p == null || s.length() == 0 || p.length() == 0) {
            return res;
        }
        int[] hash = new int[26];
        for (int i = 0; i < p.length(); i++) {
            hash[p.charAt(i) - ‘a‘]++;
        }
        int left = 0, right = 0, count = p.length();
        while (right < s.length()) {
            if (hash[s.charAt(right) - ‘a‘] >= 1) {
                count--;
            }
            hash[s.charAt(right) - ‘a‘]--;
            right++;

            if (count == 0) {
                res.add(left);
            }

            if (right - left == p.length()) {
                if (hash[s.charAt(left) - ‘a‘] >= 0) {
                    count++;
                }
                hash[s.charAt(left) - ‘a‘]++;
                left++;
            }
        }
        return res;
    }

}

还有一个问题就是

hash[s.charAt(left) - ‘a‘]++;

这句话为什么不能写在if的里面,到时候再来考虑一下。。。现在还不是很能理解。。。

时间: 2024-10-01 03:14:37

Find All Anagrams in a String Leetcode的相关文章

[leetcode] Valid Anagram 、 Find All Anagrams in a String

Valid Anagram Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false Note:

Reverse Words in a String leetcode java

题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". click to show clarification. Clarification: What constitutes a word? A sequence of non-space characters con

Scramble String leetcode java

题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choo

Interleaving String leetcode java

题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 题解: 这道题还是像

[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 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算法题-Find All Anagrams in a String(Java实现)

这是悦乐书的第228次更新,第240篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第95题(顺位题号是438).给定一个字符串s和一个非空字符串p,找到s中p的字谜的所有起始索引.字符串仅由小写英文字母组成,字符串s和p的长度不会大于20,100.输出顺序无关紧要.例如: 输入:s:"cbaebabacd" p:"abc" 输出:[0,6] 说明: 起始索引等于0的子字符串是"cba",它是"abc&quo