ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้สองสตริง s1 และ s2 รวมถึงตัวอักษรจาก ato z เท่านั้น
ฟังก์ชันของเราควรส่งคืน เรียงลำดับ . ใหม่ string ที่ยาวที่สุดเท่าที่จะเป็นไปได้ มีตัวอักษรต่างกัน - แต่ละตัวใช้เพียงครั้งเดียว - มาจาก 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