Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions strings/valid_anagram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Checks whether two strings are valid anagrams of each other.

An anagram is a word formed by rearranging the letters of another word,
using all the original letters exactly once.

For example:
listen → silent
evil → live

This can be done efficiently using Python's collections.Counter
to compare character frequencies in both strings.

Example:
>>> is_anagram("listen", "silent")
True
>>> is_anagram("evil", "vile")
True
>>> is_anagram("hello", "bello")
False
>>> is_anagram("", "")
True
>>> is_anagram("a", "aa")
False
"""

from collections import Counter


def is_anagram(s1: str, s2: str) -> bool:
"""
Returns True if the two input strings are anagrams of each other.

The comparison is case-sensitive and ignores no characters.

>>> is_anagram("listen", "silent")
True
>>> is_anagram("triangle", "integral")
True
>>> is_anagram("apple", "papel")
True
>>> is_anagram("rat", "car")
False
>>> is_anagram("a", "aa")
False
"""
return Counter(s1) == Counter(s2)


if __name__ == "__main__":
import doctest

doctest.testmod()