s and t.| Edge case | Example | Expected output |
|---|---|---|
| Both strings empty | ("", "") |
True |
| Same single character | ("a", "a") |
True |
| Different characters | ("a", "b") |
False |
| Different lengths | ("ab", "a") |
False |
| Same characters, different order | ("anagram", "nagaram") |
True |
| Different character frequencies | ("aab", "abb") |
False |
| Repeated characters | ("aacc", "ccaa") |
True |
| Large strings | Two long strings | Correct result |
Sort both strings and compare the sorted results. If the sorted strings are equal, the original strings contain exactly the same characters with the same frequencies.
Example: s = "anagram", t = "nagaram"

len(s) != len(t), return False.s: Create a sorted version of s.t: Create a sorted version of t.class Solution:
def isAnagram(self, s: str, t: str) -> bool:
# Step 1: Check lengths.
if len(s) != len(t):
return False
# Step 2-3: Sort both strings.
sorted_s = sorted(s)
sorted_t = sorted(t)
# Step 4: Compare sorted characters.
return sorted_s == sorted_t