Problem
Given two strings s and t , write a function to determine if t is an anagram of s.
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?
Example
Input: s = "anagram", t = "nagaram"
Output: true
Input: s = "rat", t = "car"
Output: false
Code
static int var = [](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size()!=t.size())
return false;
int temp[256] = {0};
for(int i=0;i<s.size();i++){
temp[s[i]]++;
temp[t[i]]--;
}
for(int i=0;i<s.size();i++){
if(temp[s[i]]!=0)
return false;
}
return true;
}
};
Result

网友评论