Problem Statement
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An anagram is a word formed by rearranging the letters of another word using all the original letters exactly once.
Approach
Count character frequencies in both strings and compare. Use a hash map or array of size 26.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | O(n) |
| Space | O(1) - constant 26 characters |
Examples
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 * 10^4 - βΈ
sandtconsist of lowercase English letters
Loading...
Sign in to run your code...
Asked by companies:
GoogleAmazonMicrosoftBloomberg