由 JavaScript 中的两个字符串构建的最长字符串
问题
我们需要编写一个JavaScript函数,该函数接受两个字符串s1和s2,其中仅包含atoz中的字母。
我们的函数应该返回一个新的排序字符串,尽可能长,包含不同的字母-每个只取一次-来自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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短