Find All Anagrams in a String
Explore how to identify all starting positions of anagrams of one string within another by tracking character frequencies efficiently. Understand how to implement this using frequency patterns and sliding window logic to solve common coding interview problems involving anagrams.
We'll cover the following...
Statement
Given two strings, a and b, return an array of all the start indexes of anagrams of b in a. We may return the answer in any order.
An anagram is a word or phrase created by rearranging the letters of another word or phrase while utilizing each of the original letters exactly once. For example, “act” is the anagram of “cat”, and “flow” is the anagram of “wolf”.
Constraints:
-
a.length,b.length - Both
aandbconsist only of lowercase English letters.
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you to check if you’re solving the correct problem:
Find All Anagrams in a String
What’s the correct output for the following inputs?
a = “abab”
b = “ab”
[0]
[0, 2]
[0, 1, 2]
[1, 2]
Figure it out
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.
import java.util.*;public class Main {public static List<Integer> findAnagrams(String a, String b) {// Replace this placeholder return statement with your codereturn new ArrayList<>();}}