题目描述
英文题目
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
1 | Input: s = "anagram", t = "nagaram" |
Example 2:
1 | Input: s = "rat", t = "car" |
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?
中文题目
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。
示例 1:
1 | 输入: s = "anagram", t = "nagaram" |
示例 2:
1 | 输入: s = "rat", t = "car" |
说明:
你可以假设字符串只包含小写字母。
进阶:
如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?
解决方法
方法一
- 描述
我们先判断两个字符串长度是否相同,不相同直接返回false。然后把s中所有的字符出现个数统计起来,存入一个大小为26的数组中,因为题目中限定了输入字符串为小写字母组成。然后我们再来统计t字符串,如果发现不匹配则返回false
- 源码
1 | bool isAnagram(char* s, char* t) { |