题目
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
解析
统计每个字符的出现次数加入set, 然后比较两set是否相等. 因为输入只包含小写字母, 可用int[26]代替.
py可以直接调用Counter.
代码
py
1 | class Solution(object): |
c++
1 | class Solution { |