#Leetcode# 242. Valid Anagram

https://leetcode.com/problems/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

代码:

class Solution {
public:
    bool isAnagram(string s, string t) {
        map<char, int> mp;
        int ls = s.length(), lt = t.length();
        if(ls != lt) return false;
        for(int i = 0; i < ls; i ++)
            mp[s[i]] ++;
        for(int i = 0; i < lt; i ++)
            mp[t[i]] --;
        
        for(int i = 0; i < ls; i ++) {
            if(mp[s[i]]) return false;
            else continue;
        }
        return true;
    }
};

  

posted @ 2018-11-15 14:51  丧心病狂工科女  阅读(96)  评论(0编辑  收藏  举报