LeetCode 242. Valid Anagram

来源:互联网 发布:java pop3 编辑:程序博客网 时间:2024/05/22 13:16

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-Europeanlanguages, but it is necessary for some other languages, such as Greek, Chinese and Japanese. Many analysts believe that as thesoftware industry becomes increasingly global, Unicode will eventually supplant ASCII as the standard character codingformat.


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;

0 0
原创粉丝点击