4.3 KiB
Valid Anagram
Problem Number: 242 Difficulty: Easy Category: Hash Table, String, Sorting LeetCode Link: https://leetcode.com/problems/valid-anagram/
Problem Description
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
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^4sandtconsist of lowercase English letters.
Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
My Approach
I used a Hash Table approach with Counter to check if two strings are anagrams. The key insight is to compare the character frequency of both strings.
Algorithm:
- Use Counter to count characters in string s
- Use Counter to count characters in string t
- Compare the two counters
- Return True if they are equal, False otherwise
Solution
The solution uses Counter to efficiently check character frequency. See the implementation in the solution file.
Key Points:
- Uses Counter for character frequency counting
- Simple one-line comparison
- Handles all edge cases automatically
- Efficient and readable approach
Time & Space Complexity
Time Complexity: O(n)
- Counter creation: O(n) for each string
- Comparison: O(k) where k is number of unique characters
- Total: O(n)
Space Complexity: O(k)
- Counter stores frequency of each unique character
- k is the number of unique characters
- In worst case: O(n)
Key Insights
-
Character Frequency: Anagrams have the same character frequency.
-
Counter Usage: Using Counter provides efficient character counting.
-
Simple Comparison: Direct comparison of counters is sufficient.
-
Unicode Support: Counter works with Unicode characters as well.
-
Case Sensitivity: The problem specifies lowercase English letters.
-
Length Check: Anagrams must have the same length.
Mistakes Made
-
Sorting Approach: Initially might sort strings, leading to O(n log n) complexity.
-
Manual Counting: Manually counting characters instead of using Counter.
-
Complex Logic: Overcomplicating the anagram check.
-
Wrong Comparison: Not using the right data structure for comparison.
Related Problems
- Group Anagrams (Problem 49): Group strings by anagrams
- Find All Anagrams in a String (Problem 438): Find anagram substrings
- Valid Parentheses (Problem 20): Check valid parentheses
- Isomorphic Strings (Problem 205): Check string isomorphism
Alternative Approaches
- Sorting: Sort both strings and compare - O(n log n) time, O(n) space
- Hash Map: Use manual hash map for counting - O(n) time, O(n) space
- Array Counting: Use array for ASCII characters - O(n) time, O(1) space
Common Pitfalls
- Sorting Usage: Using sorting when Counter is more efficient.
- Manual Counting: Manually counting characters instead of using Counter.
- Complex Logic: Overcomplicating the anagram check.
- Wrong Data Structure: Not using appropriate data structure for counting.
- Case Sensitivity: Not handling case sensitivity properly.
Note: This is a simple hash table problem that demonstrates efficient anagram checking with Counter.