문제
우리는 ato z에서 문자만 포함하여 두 개의 문자열 s1 및 s2를 취하는 JavaScript 함수를 작성해야 합니다.
우리의 함수는 새로운 정렬된 을 반환해야 합니다. s1 또는 s2에서 가져온 고유한 문자를 포함하는 가능한 가장 긴 문자열(각각 한 번만 사용됨).
예시
다음은 코드입니다 -
const str1 = "xyaabbbccccdefww";
const str2 = "xxxxyyyyabklmopq";
const longestPossible = (str1 = '', str2 = '') => {
const combined = str1.concat(str2);
const lower = combined.toLowerCase();
const split =lower.split('');
const sorted = split.sort();
const res = [];
for(const el of sorted){
if(!res.includes(el)){
res.push(el)
}
}
return (res.join(''));
};
console.log(longestPossible(str1, str2)); 출력
다음은 콘솔 출력입니다 -
abcdefklmopqwxy