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.

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

Time Complexity will be O(n) because the "start" and "end" points will only move from left to right once.

Sliding Window: Use a count to denote the difference between current sliding window and p, if count == 0, means the current sliding window is the same with p, add start to the result

 1 public class Solution {
 2     public List<Integer> findAnagrams(String s, String p) {
 3         List<Integer> res = new ArrayList<Integer>();
 4         if (s==null || s.length()==0 || p==null || p.length()==0 || p.length()>s.length()) return res;
 5         int[] chars = new int[26];
 6         for (int i=0; i<p.length(); i++) {
 7             chars[p.charAt(i)-‘a‘]++;
 8         }
 9         int start = 0, end = 0, count = p.length();
10         while (end < s.length()) {
11             if (end-start==p.length() && chars[s.charAt(start++)-‘a‘]++>=0) count++;
12             if (--chars[s.charAt(end++)-‘a‘] >= 0) count--;
13             if (count == 0) res.add(start);
14         }
15         return res;
16     }
17 }
时间: 2024-07-30 20:30:05

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

[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] 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:

【Leetcode】Group Anagrams

题目链接:https://leetcode.com/problems/anagrams/ 题目: Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", &q

【leetcode】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

LeetCode OJ1:Reverse Words in a String

问题描述: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 解题思路: 先利用split()方法将句子按空格分为几个单词的列表, 然后reverse()函数使列表逆序, 最后join函数以空格为间隔拼成字符串返回结果. Python代码: class Solution:  

LeetCode OJ - Reverse Words in a String

题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 解题思路: 1.先对字符串进行一次总的翻转 2.以空格为单位,划分单词,然后对每个单词再进行一次翻转 代码: class Solution { public: void swap(char& a, char

【leetcode刷题笔记】Interleaving String

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. 题解:DP问题. 用数组dp[i][

【leetcode刷题笔记】Scramble String

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 choose a