[leetcode]-242. 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

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?

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

猜你喜欢

转载自blog.csdn.net/shen_zhu/article/details/81431950