力扣 | 242. 有效的字母异位词

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

示例 1:

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

输入: s = "rat", t = "car"
输出: false
说明:
你可以假设字符串只包含小写字母。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/valid-anagram

bool isAnagram(char *s,char *t)
{
	int statS[26]={0};
	int statT[26]={0};
	int lenS = strlen(s);
	int lenT = strlen(t);
	int i;
	for(i=0;i<lenS;++i)
	{
        //ASCII码确定下标
		int index = s[i]-'a';
		statS[index]++;
	}	
	for(i=0;i<lenT;++i)
	{
		int index = t[i]-'a';
		statT[index]++;
	}
	for(i=0;i<26;i++)
	{
		if(statS[i] != statT[i])
		{
			return false;
		}
	}
	return true; 
} 

  

猜你喜欢

转载自www.cnblogs.com/chrysanthemum/p/11819134.html