Problem Statement
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An anagram means both strings contain the same characters with the same frequency, just arranged differently.
Examples:
Input: s = "anagram", t = "nagaram"
Output: true
Input: s = "rat", t = "car"
Output: false
My Goal
For this problem, my goal was to:
Understand how character frequency works in strings
Compare two strings efficiently
Avoid unnecessary complexity
Use simple and readable logic
Solution
I used a sorting-based approach, which is simple and beginner-friendly.
Idea:
Sort both strings
If they are equal → they are anagrams
Otherwise → not anagrams
Solution Code (Python)
s = "anagram"
t = "nagaram"
print(sorted(s) == sorted(t))
Explanation
sorted(s) rearranges characters of string s
sorted(t) rearranges characters of string t
If both sorted results are equal → same characters → anagram
Top comments (0)