389. Find the Difference

来源:互联网 发布:淘宝cvr 编辑:程序博客网 时间:2024/05/01 21:31

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:第一遍提交的时候没有把没用到的头文件

0 0
原创粉丝点击