LeetCode 242. Valid Anagram

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;

你可能感兴趣的:(LeetCode 242. Valid Anagram)