Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return 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?
UniCode Characters VS Ascii Characters.
A standard for representing characters as integers. Unlike ASCII, which uses 7 bits for each character, Unicode uses 16 bits, which means that it can represent more than 65,000 unique characters. This is a bit of overkill for English and Western-European languages, but it is necessary for some other languages, such as Greek, Chinese and Japanese. Many analysts believe that as the software industry becomes increasingly global, Unicode will eventually supplant ASCII as the standard character coding format.
Method1:
bool isAnagram(string s, string t) {
if(s.size() != t.size()) return false; // anagrams should have the same size.
sort(s.begin(), s.end());
sort(t.begin(), t.end()); // anagrams after sorting should be the same.
return s == t;
}
Method2: HashMap.... unordered_map<int, int> CharactersToCount;
本文介绍了一种方法来确定两个字符串是否为彼此的字谜。通过排序和比较两个字符串,可以有效地解决这一问题。此外,还探讨了当输入包含Unicode字符时如何调整解决方案。
676

被折叠的 条评论
为什么被折叠?



