Valid Anagram || Rearranging the Letters of A Different Word or Phrase

Valid Anagram

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: s = "anagram", t = "nagaram"Output: true Example 2:

Input: s = "rat", t = "car"Output: false

Constraints:

1 <= s.length, t.length <= 5 * 104s and t consist of lowercase English letters.

Sample input 

anagram

nagaram

Sample output

true




Program:

 def isAnagram(s , t):

    if len(s) != len(t):

        return False


    count = {}

    for char in s:

        if char in count:

            count[char] += 1

        else:

            count[char] = 1


    for char in t:

        if char not in count:

            return False

        else:

            count[char] -= 1

            if count[char] == 0:

                del count[char]


    return len(count) == 0


s = input("Enter the first string: ")

t = input("Enter the second string: ")


print(isAnagram(s, t))









Explanation:

Code Breakdown
The task is to determine if two given strings are anagrams of each other. An anagram is a word or phrase that's formed by rearranging the letters of another word or phrase. For example, 'cinema' is an anagram of 'iceman'.

Function Declaration
The function isAnagram(s, t) is defined to take two arguments: s and t. These are the two strings that we'll check to see if they are anagrams of each other.

Length Check
The first line inside the function is a quick check to see if the lengths of s and t are equal. If they aren't, the function immediately returns False because if two strings are different lengths, they can't possibly be anagrams.

Frequency Count
The next block of code creates a Python dictionary count, which keeps track of the frequency of each character in string s. We iterate through each character in s, and for each character, we add it to the dictionary and increase its count.

Validation
We then loop over each character in string t. For every character, we decrease its count in the count dictionary. If the character from t is not found in the count dictionary, it means that t has a character that doesn't exist in s, so we return False. If the count of a character in the count dictionary reaches 0, we remove that entry from the dictionary.

At the end of these operations, if t is an anagram of s, the count dictionary should be empty, which means the function returns True. Otherwise, it returns False.

User Input and Result
After defining the function, we prompt the user to input two strings. We then call isAnagram(s, t) with these strings and store the result.

The final line of the code prints "true" if the result is True (indicating the strings are anagrams), and "false" if the result is False (indicating the strings are not anagrams).

And there you have it! This Python code neatly checks if two strings are anagrams of each other in a highly efficient way.

Post a Comment

0 Comments