您的位置:首页 > Web前端

389. Find the Difference

2017-03-12 14:39 225 查看
Difficulty: Easy

Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

Example:

Input:

s = “abcd”

t = “abcde”

Output:

e

Explanation:

‘e’ is the letter that was added.

c语言

//#include <string.h>
char findTheDifference(char* s, char* t) {
int count_s[26] = {0};
int count_t[26] = {0};
int i=0;
//int ls, lt;
//ls = strlen(s);
//lt = strlen(t);
while(t[i] != '\0') {
count_t[t[i] - 'a']++;
i++;
}
i = 0;
while(s[i] != '\0') {
count_s[s[i]-'a']++;
i++;
}
for(i = 0; i < 26; i++) {
if (count_s[i] != count_t[i]) return i+'a';
}
return NULL;
}


ps:第一遍提交的时候没有把没用到的头文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode