leetcode Valid Anagram

来源:互联网 发布:linux gpu使用率 编辑:程序博客网 时间:2024/06/05 04:01

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

For example,
s = "anagram", t = "nagaram", return true.

s = "rat", t = "car", return false.



用一个26位长度的桶来装26个小写字母。遍历s时,统计所有的字符分布。

遍历t时,按照字母依次减去每个字符的统计。

如果是Anagram,那最后所有元素都为0.


bool isAnagram(char* s, char* t) {    char dic[26]={0};        while(*s){        dic[*s-'a']++;        s++;    }        while(*t){        dic[*t-'a']--;        t++;    }        for(int i=0;i<26;i++)    {        if(dic[i]!=0)            return false;    }    return true;}


0 0
原创粉丝点击