由 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 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短