题目描述
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?
我的解法
解题思路
题目要求判断两个字符串是否是以同样的字符组成
实现代码
1 | class Solution { |
Runtime: 16 ms, faster than 55.14% of C++ online submissions for Valid Anagram.
Memory Usage: 9.5 MB, less than 74.63% of C++ online submissions for Valid Anagram.
高票解法
因为只有小写的字母,所以可以用一个数组来统计每个字符出现的次数
实现代码
1 | class Solution { |
Runtime: 8 ms, faster than 97.83% of C++ online submissions for Valid Anagram.
Memory Usage: 9.6 MB, less than 55.22% of C++ online submissions for Valid Anagram.