문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
두 개의 문자열 s와 t를 갖게 된다.
문자열 t는 문자열 s를 무작위로 섞은 후 임의의 위치에 문자 하나를 추가하여 생성된다.
t에 추가된 문자를 반환한다.
#1
Input: s = "abcd", t = "abcde"
Output: "e"
Explanation: 'e'는 추가된 글자이다.
#2
Input: s = "", t = "y"
Output: "y"
class Solution {
public char findTheDifference(String s, String t) {
Map<Character, Integer> count = new HashMap<>();
for(char c : t.toCharArray()){
count.put(c, count.getOrDefault(c, 0) + 1);
}
for(char c : s.toCharArray()){
count.put(c, count.get(c) - 1);
if(count.get(c) == 0){
count.remove(c);
}
}
return (char) count.keySet().toArray()[0];
}
}