[LeetCode]Valid Anagram

来源:互联网 发布:js user strict 编辑:程序博客网 时间:2024/05/22 17:21

题目:判断两个字符串中的字母是否一致

分析:遍历两个串,记录每个字母的数量,最后比较各个字母的数量。

参考代码:

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


}

0 0
原创粉丝点击