Skip to content

[LeetCode] 389. 找不同 #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Animenzzzz opened this issue Sep 3, 2019 · 0 comments
Open

[LeetCode] 389. 找不同 #64

Animenzzzz opened this issue Sep 3, 2019 · 0 comments

Comments

@Animenzzzz
Copy link
Owner

题目描述:

给定两个字符串 s 和 t,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

示例:

输入:
s = "abcd"
t = "abcde"

输出:
e

解释:
'e' 是那个被添加的字母。

解题思路:使用哈希表,遍历一遍存数。使用自加来记录出现次数,当为0时,说明没有记录过或者已经超过其重复次数了。test case: s:a. t:aa

C++解题:

class Solution {
public:
    char findTheDifference(string s, string t) {
        unordered_map<char,int> map;
        for(char cc : s) map[cc]++;
        for(char cc:t){
            if(map[cc] == 0) return cc;
            else map[cc]--;
        } 
        return ' ';
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant