LeetCode 242
Valid Anagram
- Problem Description:
给出两串字符串,判断第二串字符串是否由第一串字符串乱序得到的。
具体的题目信息:
https://leetcode.com/problems/valid-anagram/description/ - Example:
- Solution:(挺精致的答案)
class Solution {
public:
bool isAnagram(string s, string t) {
if (s.length() != t.length()) return false;
if (s == "" && t == "") return true;
map<char, int> res;
for (int i = 0; i < s.length(); i++) {
res[s[i]]++;
res[t[i]]--;
}
for (int i = 0; i < res.size(); i++) {
if (res[s[i]])
return false;
}
return true;
}
};