How to Check if Two Strings Are AnagramAn anagram is a literary device. In this device, two words are called anagrams of each other if the alphabets of one of the words are rearranged letters of the other word. Hence, the list of letters of both words should be the same. For example, the string 'abcde' and the string 'bdeac' are anagrams of each other. These are anagrams because the list of letters in both strings is the same, i.e., ['a', 'b', 'c', 'd', 'e']. In this problem, we are given two strings. Our task is to tell if the two strings are anagrams or not. Let us see some examples to understand the problem. Input: s1 = "listen" s2 = "silent" Output: Anagram Explanation: The list of characters of the words are the same, i.e., ['l', 'i', 'e', 'n', 't', 's']. Input: s1 = "gram" s2 = "arm" Output: Not an anagram Explanation: The list of characters of the word "gram" is ['g', 'r', 'a', 'm'] and the list of characters of the word "arm" is ['r', 'a', 'm']. Since the list is not the same, therefore, the two words are not anagrams. Approach - 1In this approach, we will use a basic approach to solve the problem. We will sort the strings, and if the sorted strings are equal, then the strings are anagrams. Here is the algorithm of this approach:
Below is the implementation of this approach in Python. Code Output: The given two strings are not anagrams Time Complexity: This approach has logarithmic time complexity. To perform sorting, the time complexity is O(log N). Space Complexity: We have not used any extra space; hence, the space complexity is O(1). Approach - 2In this approach, we will check if the strings are anagrams or not by counting the frequency of letters. We will count the frequency of each character of the two strings, and if the strings contain same characters and the frequency of each character matches, then the given strings are anagrams. Let us see the algorithm of this approach:
Below is the implementation of the above-mentioned approach. Code Time Complexity: This approach has logarithmic time complexity. To perform sorting, the time complexity is O(log N). Space Complexity: We have not used any extra space; hence, the space complexity is O(1). Approach - 2In this approach, we will check if the strings are anagrams or not by counting the frequency of letters. We will count the frequency of each character of the two strings, and if the strings contain same characters and the frequency of each character matches, then the given strings are anagrams. Let us see the algorithm of this approach:
Below is the implementation of the above-mentioned approach. Code Output: The given two strings are not anagrams Time Complexity: We have used linear loops to solve this problem hence the time complexity is O(n) Auxiliary Space: We have created two arrays to store the frequency of the letters, so the space complexity is O(N), where N is the total count of characters, which is 256. Approach - 3In this approach, we will check if the given two strings are anagrams of each other or not using HashSet. This approach is the optimized solution of the above approach. We do not need to create the count arrays of length 256. We can use a hashset to store the frequencies of each character. In this approach, we will not create two hash sets; instead, we will use only one HashSet. We will insert the frequency of characters of one string. Then, we will iterate over the other string and decrease the frequency of the characters. If, at last, the frequency of all the characters is zero, then the given strings are anagrams of each other. If this is not true then the strings are not anagrams. Here is the algorithm of this approach:
Below is the implementation of the above-mentioned approach. Code Output: The given two strings are not anagrams Time Complexity: The time complexity of this approach is linear because we have used linear loops to solve the problem. Hence, the time complexity is O(N). Space Complexity: The space complexity of this program is less as compared to the previous program. In this approach, we have created only one hashset of length N. Therefore, the space complexity is O(N). Next TopicHow to find the largest bst in bt |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India