15: Check if two strings are anagrams of each other.
⏳⌚ 00:00:00

Question:-

Check if two strings are anagrams of each other.
Example 1:
Input:
s1 = "hello" s2 = "elloh"
Output: "yes"
Example 2:
Input:
s1 = "world" s2 = "elloh"
Output: "No"

Steps to solve:-

1. areAnagram Function:- The areAnagram function takes two input strings, s1 and s2, as parameters.
It calculates the lengths of both strings (n1 and n2) and checks if they have the same length. If the lengths are different, the function returns false because they cannot be anagrams.
Both strings are sorted using the sort function, which rearranges the characters in ascending order.
It then iterates through both sorted strings and compares each character at the same position. If any character differs, the function returns false.
If all characters match, the function returns true, indicating that the input strings are anagrams.
2. main function:-The main function reads two strings, s1 and s2, from the user.
It calls the areAnagram function to check if the two input strings are anagrams.
If the function returns true, it prints "Yes" to indicate that they are anagrams. Otherwise, it prints "No" to indicate that they are not anagrams.

solution->

View Code 1
Time Complexity = O(n * log(n))
Space Complexity = O(n1 + n2)