Valid Anagram
Learn how to determine if two strings are valid anagrams by efficiently tracking character frequencies. This lesson helps you understand the problem constraints and implement solutions that check for rearranged letters using pattern-based problem-solving techniques. Explore how to apply data tracking methods to solve permutation and anagram problems effectively.
We'll cover the following...
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:
-
str1.length,str2.length -
str1andstr2consist 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
What’s the correct output for the following inputs?
str1 = “heart”
str2 = “earth”
TRUE
FALSE
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.
def is_anagram(str1, str2):# Replace this placeholder return statement with your codereturn False