美文网首页
242. Valid Anagram

242. Valid Anagram

作者: SilentDawn | 来源:发表于2018-09-16 09:23 被阅读0次

Problem

Given two strings s and t , write a function to determine if t is an anagram of s.
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?

Example

Input: s = "anagram", t = "nagaram"
Output: true
Input: s = "rat", t = "car"
Output: false

Code

static int var = [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.size()!=t.size())
            return false;
        int temp[256] = {0};
        for(int i=0;i<s.size();i++){
            temp[s[i]]++;
            temp[t[i]]--;
        }
        for(int i=0;i<s.size();i++){
            if(temp[s[i]]!=0)
                return false;
        }
        return true;
    }
};

Result

242. Valid Anagram.png

相关文章

网友评论

      本文标题:242. Valid Anagram

      本文链接:https://www.haomeiwen.com/subject/qnyxnftx.html