leetcode-242. Valid Anagram

来源:互联网 发布:软件研发的特点 编辑:程序博客网 时间:2024/06/05 00:35

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.

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?

思路:考虑unicode的话,要么加存储数组,空间过大的话可以考虑先排序再比较的方法

#define CHAR_NUM 26#define BEGIN_CHAR ('a')bool isAnagram(char* s, char* t) {    int length = strlen(s);    int i = 0;    int charNum[CHAR_NUM] = {0};    if(length != strlen(t))    {        return false;    }    for(i = 0;i < length;i++)    {        charNum[s[i]- BEGIN_CHAR]++;        charNum[t[i]- BEGIN_CHAR]--;    }    for(i = 0;i < CHAR_NUM;i++)    {        if(charNum[i] != 0)        {            return false;        }    }    return true;}
0 0