Grokking the Coding Interview: Patterns for Coding Questions

0% completed

Solution: Problem Challenge 2: String Anagrams

Problem Statement

Given a string and a pattern, find all anagrams of the pattern in the given string.

Every anagram is a permutation of a string. As we know, when we are not allowed to repeat characters while finding permutations of a string, we get N! permutations (or anagrams) of a string having N characters. For example, here are the six anagrams of the string abc:

  1. abc
  2. acb
  3. bac
  4. bca
  5. cab
  6. cba

Write a function to return a list of starting indices of the anagrams of the pattern in the given string.

Example 1:

.....

.....

.....

Like the course? Get enrolled and start learning!
D

Dee

· 4 years ago

Why is matched compared to the number of keys in the charFrequency instead of the length of the pattern? Can the pattern not have repeating characters such as “ aabc” vs just “abc”?

Show 2 replies