[LeetCode] 242. Valid Anagram (C++)

版权声明:本文为博主原创文章,未经博主允许不得转载。@ceezyyy11 https://blog.csdn.net/ceezyyy11/article/details/88919128

[LeetCode] 242. Valid Anagram (C++)

Easy

Share
Given two strings s and t , 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> m1;
        map<char,int> m2;
        for(char c:s) {
            m1[c]++;
        }
        for(char c:t) {
            m2[c]++;
        }
        for(char c='a';c<='z';c++) {
            if(m1[c]!=m2[c]) {
                return false;
            }   
        }
        return true;
    }
};


/*
Submission Detail:

Runtime: 20 ms, faster than 45.37% of C++ online submissions for Valid Anagram.
Memory Usage: 9.5 MB, less than 5.16% of C++ online submissions for Valid Anagram.

*/


/*
Study Notes:

Anagram: https://en.wikipedia.org/wiki/Anagram
From Wikipedia, the free encyclopedia

*/

猜你喜欢

转载自blog.csdn.net/ceezyyy11/article/details/88919128