How to Check if Two Strings are Anagrams

91 views

How to Check if Two Strings are Anagrams

How to Check if Two Strings are Anagrams

I want to determine if two strings are anagrams of each other (i.e., contain the same characters in a different order). How can I check this?

solveurit24@gmail.com Changed status to publish February 16, 2025
0

Sort both strings and compare the results.

Code Example:

def are_anagrams(str1, str2):
    return sorted(str1) == sorted(str2)

# Example usage
print(are_anagrams("listen", "silent"))  # Output: True

This function checks if both strings have the same characters by comparing their sorted versions.

solveurit24@gmail.com Changed status to publish February 16, 2025
0