Leetcode之Valid Anagram

題目:

Given two strings s and , write a function to determine if t is an anagram of s.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: 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?

代碼:

class Solution {
public:
    bool isAnagram(string s, string t) {
        map<char, int> m;
	int len1 = s.length(), len2 = t.length();
	if (len1 != len2)return false;
	for (int i = 0; i < len1; i++) {
		m[s[i]] += 1;
		m[t[i]] -= 1;
	}
	for (auto a = m.begin(); a != m.end(); a++) {
		if (a->second != 0)return false;
	}
	return true;
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章