Valid Anagram

Try to solve the Valid Anagram problem.

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#

Created with Fabric.js 3.6.6 Input 2 Input 1 Output str1 s p a r e str2 p e a r s Output TRUE Sample example 1 ------------------------------------------------------------

1 of 2

Created with Fabric.js 3.6.6 Input 2 Input 1 Output str1 s u p e r str2 u p p e r Output FALSE Sample example 2 ------------------------------------------------------------

2 of 2

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

Question 1 of 30 attempted

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.

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

Check whether the lengths of the strings are equal. If not, return FALSE.

Initialize a hash map to store the character as a key and the count of the respective character as a corresponding value.

Iterate over the characters in the string str1, and increment the count for each character.

Iterate over the characters in the string str2, and decrement the count for each character.

If the count for all the characters is zero, return TRUE. Otherwise, return FALSE.


Try it yourself#

Implement your solution in the following coding playground.

Python
usercode > main.py
Input #1
Input #2
Valid Anagram

Solution: Palindrome Permutation

Solution: Valid Anagram