242. 有效的字母异位词

26 阅读1分钟

242. 有效的字母异位词

已解答

简单

相关标签

premium lock icon相关企业

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的 字母异位词。

 

示例 1:

输入: s = "anagram", t = "nagaram"
输出: true

示例 2:

输入: s = "rat", t = "car"
输出: false

 

提示:

  • 1 <= s.length, t.length <= 5 * 104
  • s 和 t 仅包含小写字母

 

进阶: 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?

题解:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char s[50000] = {'\0'};
    char t[50000] = {'\0'};
    scanf("%s", s);
    scanf("%s", t);
    int cal[26] = {0};
    int i;
    i = 0;
    while (s[i] != '\0')
    {
        cal[s[i] - 'a'] ++;
        i ++;
    }
    i = 0;
    while (t[i] != '\0')
    {
        if (cal[t[i] - 'a'] > 0)
        {
            cal[t[i] - 'a'] --;
        }
        else
        {
            printf("false");
            return 0;
        }
        i ++;
    }
    printf("true");
    return 0;
}