Search⌘ K
AI Features

Valid Anagram

Explore how to verify if one string is an anagram of another by tracking character frequencies. Understand constraints and implement an efficient solution to this classic problem, building skills with frequency tracking patterns useful in coding interviews.

Statement

Given two strings, str1 and str2, check whether str2 is an anagram of str1.

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.

Constraints:

  • 11 \leq str1.length, str2.length 103\leq 10^3

  • str1 and str2 consist of only 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 check if you’re solving the correct problem:

Valid Anagram

1.

What’s the correct output for the following inputs?

str1 = “heart”

str2 = “earth”

A.

TRUE

B.

FALSE


1 / 3

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.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in the following coding playground.

usercode > Main.java
import java.util.*;
public class Main{
public static boolean isAnagram(String str1, String str2) {
// Replace this placeholder return statement with your code
return false;
}
}
Valid Anagram