Find the Difference
4830 단어 leetcodeJavaScriptJavaScript
📚 문제
You are given two strings s and t.
String t is generated by random shuffling string s and then add one more letter at a random position.
Return the letter that was added to t.
Example 1:
Input: s = "abcd", t = "abcde"
Output: "e"
Explanation: 'e' is the letter that was added.
Example 2:
Input: s = "", t = "y"
Output: "y"
Constraints:
0 <= s.length <= 1000
t.length == s.length + 1
s and t consist of lowercase English letters.
💡 풀이
var findTheDifference = function (s, t) {
// s와 t의 요소들을 배열로 넣고 알파벳 순으로 정렬한다
// t 배열을 반복문을 돌리면서 s에 요소가 없을 경우 그 요소를 리턴한다
let sSort = s.split("").sort();
let tSort = t.split("").sort();
for (let i = 0; i < tSort.length; i++) {
if (tSort[i] !== sSort[i]) {
return tSort[i];
}
}
};
✅ 문자열로는 알파벳 순 정렬이 안되니까 배열로 바꿔서 정렬하고 반복문으로 두개의 배열 비교하면 풀리는 문제!
Author And Source
이 문제에 관하여(Find the Difference), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jamiep9rk/Find-the-Difference저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)