题目来源:
力扣https://leetcode.cn/problems/find-the-difference/题目简介:
就是t字符串是由s字符串重排,并且在t字符串中我们在随机位置插入了一个字母,找出并返回这个字母。
思路:
还是和之前的差不多,就是维护一个26字长的数组,用memset给各个位置赋0,然后遍历s字符串,然后投进去,然后再遍历一次t字符串,用已经存好的table来做减法,找到<0的位置就返回这个位置的字符。
代码实现:
char findTheDifference(char * s, char * t){
int cnt[26];
int len_s=strlen(s), len_t=strlen(t);
memset(cnt, 0, sizeof(cnt));
for(int i=0;i<len_s;++i){
cnt[s[i]-'a']++;
}
for(int i=0;i<len_t;++i){
cnt[t[i]-'a']--;
if(cnt[s[i]-'a']<0){
return t[i];
}
}
return false;
}