250x250
반응형
Notice
Recent Posts
Recent Comments
Link
N
(Leet Code JS)Find the Difference 본문
728x90
반응형
389. Find the Difference
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"
Example 3:
Input: s = "a", t = "aa" Output: "a"
Example 4:
Input: s = "ae", t = "aea" Output: "a"
Constraints:
- 0 <= s.length <= 1000
- t.length == s.length + 1
- s and t consist of lower-case English letters.
우선 주어진 문자열 s와 t를 배열로 바꿔준다.
그 후 각각 오름차순으로 정렬을 한 다음,
for문으로 다른 부분이 있는 지 확인하면 된다.
만약 for문을 모두 돌아도 없다면 ts의 가장 마지막 원소를 리턴하면 된다.
/**
* @param {string} s
* @param {string} t
* @return {character}
*/
var findTheDifference = function(s, t) {
const ss = s.split("");
const ts = t.split("");
ss.sort();
ts.sort();
for(let i = 0; i < ss.length; i++){
if(ss[i] !== ts[i]){
return ts[i];
}
}
return ts[ts.length - 1];
};
728x90
반응형
'Leet Code 알고리즘' 카테고리의 다른 글
(Leet Code JS)Island Perimeter (0) | 2021.09.06 |
---|---|
(Leet Code JS)Validate Binary Search (0) | 2021.09.06 |
(Leet Code JS)Merge Intervals (0) | 2021.08.26 |
(Leet Code c++)Merge Intervals (0) | 2021.08.26 |
(Leet Code JS)Roman to Integer (0) | 2021.08.25 |