leetcode-Valid Anagram

来源:互联网 发布:红外光谱分析软件 编辑:程序博客网 时间:2024/06/08 15:58
Difficulty: Easy

Given two strings s and t, write a function to determine ift 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

class Solution {public:    bool isAnagram(string s, string t) {        map<char,int> check;        int nonZero=0;        for(auto &e:s)            if(++check[e]==1)                ++nonZero;        for(auto &e:t){            if(--check[e]==0)                --nonZero;            else if(check[e]==-1)                ++nonZero;        }        return nonZero==0;            }};


0 0
原创粉丝点击