给定两个字符串 s 和 t,它们只包含小写字母。
字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
请找出在 t 中被添加的字母。
input:s = "abcd", t = "abcde" output:"e"
class Solution {
public char findTheDifference(String s, String t) {
Map<Character,Integer> map = new HashMap<Character,Integer>();
for(char ch : s.toCharArray()){
int count = map.getOrDefault(ch,0);
map.put(ch,count + 1);
}
for(char ch : t.toCharArray()){
int count = map.getOrDefault(ch,0);
if(count == 0){
return ch;
} else {
map.put(ch,count - 1);
}
}
return ' ';
}
}