हमें एक JavaScript फ़ंक्शन लिखना है जो एकमात्र तर्क के रूप में एक स्ट्रिंग लेता है। फ़ंक्शन को स्ट्रिंग्स की एक सरणी उत्पन्न करनी चाहिए जिसमें सरणी में मौजूद सभी संभावित सन्निकट उपस्ट्रिंग्स हों।
उदाहरण
निम्नलिखित कोड देखें:
const str = 'Delhi';
const allCombinations = (str1 = '') => {
const arr = [];
for (let x = 0, y = 1; x < str1.length; x++, y++) {
arr[x] = str1.substring(x, y);
};
const combination = [];
let temp = "";
let len = Math.pow(2, arr.length);
for (let i = 0; i < len; i++) {
temp = "";
for (let j = 0; j < arr.length; j++) {
if ((i & Math.pow(2, j))) {
temp += arr[j];
}
};
if (temp !== "") {
combination.push(temp);
}
}
return combination;
};
console.log(allCombinations(str));
आउटपुट
कंसोल पर निम्नलिखित आउटपुट प्राप्त होगा:
[
'D', 'e', 'De', 'l',
'Dl', 'el', 'Del', 'h',
'Dh', 'eh', 'Deh', 'lh',
'Dlh', 'elh', 'Delh', 'i',
'Di', 'ei', 'Dei', 'li',
'Dli', 'eli', 'Deli', 'hi',
'Dhi', 'ehi', 'Dehi', 'lhi',
'Dlhi', 'elhi', 'Delhi'
]