Knowee
Questions
Features
Study Tools

Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Question

Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python solution for the problem:

def findAnagrams(s, p):
    from collections import Counter
    pCounter = Counter(p)
    sCounter = Counter(s[:len(p)-1])
    result = []
    for i in range(len(p)-1,len(s)):
        sCounter[s[i]] += 1   # include a new char in the window
        if sCounter == pCounter:    # This step is O(1), since there are at most 26 English letters 
            result.append(i-len(p)+1)   # append the starting index
        s

This problem has been solved

Similar Questions

Given an array of strings strs, group the anagrams together. You can return the answer in any order.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.Note: Use Structure

Given a string S. the task is to determine if there exists another string that is an anagram of S.An anagram is a word or phrase formed by rearranging the letters of another word or phrase. For example "seekg" is an anagram of "geeks".Example 1:Input: S = "ab"Output: YESExplanation:The string "ba" is not identical to S and is an anagram of S

An anagram is a word or a phrase made by transposing the letters of another word or phrase; for example, "parliament" is an anagram of "partial men," and "software" is an anagram of "swear oft." Write a program that figures out whether one string is an anagram of another string. The program should ignore white space and punctuation

wo words are anagrams if you can rearrange the letters from one to spell the other. For example, `'hello'` and `'olleh'` are anagrams. You are to write a function called `isAnagram1` which takes two strings and returns `True` if they are anagrams. You can assume that the strings are made up of lowercase alphabets. `isAnagram1` should make use of the `sorted` function.

Write a program to check two strings are anagram or not.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.