Problem Statement
You’re given two strings s1 and s2, you need to check whether the two strings are anagrams of each other or not.
Example 1: Let s1 = “creative” and s2 = “reactive”.
Since the second string can be formed by rearranging the letters of the first string and vice-versa, thus the two strings are anagrams of each other.
Example 2: Let s1 = “Peter Piper picked a peck of pickled peppers” and s2 = “A peck of pickled peppers Peter Piper picked”.
Since the second string can’t be formed by rearranging the letters of the first string and vice-versa, thus the two strings aren’t anagrams of each other.
Process For Checking if Two Strings Are Anagrams of Each Other
You can follow the approach below to check if the two strings are anagrams of each other:
Compare the length of both strings. If the length of both strings is not the same, it means they can’t be anagrams of each other. Thus, return false. If the length of both strings is the same, proceed further. Sort both strings. Compare both sorted strings. If both the sorted strings are the same, it means they’re anagrams of each other. Thus, return true. If both the sorted strings are different, it means they’re not anagrams of each other. Thus, return false.
C++ Program to Check If Two Strings Are Anagrams of Each Other
Below is the C++ program to check if two strings are anagrams of each other or not:
Output:
Python Program to Check Whether Two Strings Are Anagrams of Each Other
Below is the Python program to check if two strings are anagrams of each other or not:
Output:
Check If Two Strings Are Anagrams of Each Other in JavaScript
Below is the JavaScript program to check if two strings are anagrams of each other or not:
Output:
Use the Right Resources to Learn to Code
If you’re looking to solidify your coding skills, it’s important to learn new concepts and spend time using them. One way to do this is with programming apps, which will help you learn different programming concepts while having fun at the same time.