알고리즘/leetcode
[LeetCode][JavaScript] 389. Find the Difference
CooNiHong
2021. 7. 29. 14:31
[LeetCode][JavaScript] 389. Find the Difference
문제 출처: https://leetcode.com/problems/find-the-difference/
Find the Difference - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
[직접 푼 코드]
var findTheDifference = function (s, t) {
s = s.split('').sort();
t = t.split('').sort();
for (let i = 0; i < s.length; i++) {
if (s[i] != t[i]) return t[i];
}
return t[t.length - 1];
};
[문제 풀이]
1. 다른 한글자를 찾아야하기 때문에 두 문자열을 정렬합니다. (sort 메서드)
2. 반복문을 활용해 1번 문자열과 2번 문자열의 같은 자릿수에서 다른 글자가 있을 경우 그 문자를 반환합니다.
3. 1번 문자열의 길이가 0 or 1일 경우 2번 문자열의 맨뒤 글자를 반환합니다.
알고리즘 스터디: https://github.com/ROUTINE-STUDY/Algorithm
GitHub - ROUTINE-STUDY/Algorithm: 초보 알고리즘 스터디 / 누구나 참여 가능
초보 알고리즘 스터디 / 누구나 참여 가능 :runner:. Contribute to ROUTINE-STUDY/Algorithm development by creating an account on GitHub.
github.com
누구나 참여 가능한 알고리즘 스터디입니다.
알고리즘뿐만 아니라 개발을 하면서 겪은 이슈, 이론을 정리하는 Routine-Study를 운영 중입니다.
728x90