समस्या
हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो दो स्ट्रिंग्स s1 और s2 लेता है जिसमें केवल ato z के अक्षर शामिल हैं।
हमारे फ़ंक्शन को एक नया सॉर्ट किया गया . वापस करना चाहिए स्ट्रिंग, सबसे लंबा संभव, जिसमें अलग-अलग अक्षर होते हैं - प्रत्येक केवल एक बार लिया जाता है - 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