Problem
Given two stringss and t, return true if t is an anagram of s, and false otherwise.
Examples
Constraints
- 1 <= s.length, t.length <= 5 * 10^4
- s and t consist of lowercase English letters.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 242 with 18 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 242 # by problem number
lcpy gen -s valid_anagram # by problem name
s and t, return true if t is an anagram of s, and false otherwise.
Input: s = "anagram", t = "nagaram"
Output: true
Input: s = "rat", t = "car"
Output: false
from collections import Counter
class Solution:
# Time: O(n)
# Space: O(1) - at most 26 unique characters
def is_anagram(self, s: str, t: str) -> bool:
return Counter(s) == Counter(t)
| Time | Space |
|---|---|
| O(n) | O(1) - at most 26 unique characters |