Created
September 20, 2017 05:20
-
-
Save casprwang/12b3f13dd45a90453f3e06abc81b90ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// let str = 'abc' | |
// const logSub = s => { | |
// let res = [] | |
// for (let i =0; i<s.length; i++) { | |
// for( let j=i+1; j<=s.length;j++) { | |
// res.push(str.slice(i,j)) | |
// } | |
// } | |
// return res | |
// } | |
// console.log( | |
// logSub(str) | |
// ) | |
const log = s => { | |
let res = [] | |
const iter = (temp, left) => { | |
for (let i=0; i<left.length;i++) { | |
iter(temp+left[i], [...left.slice(0,i), ...left.slice(i+1)]) | |
} | |
res.push(temp) | |
} | |
iter('', s) | |
return res | |
} | |
console.log(log(['a','b','c'])) | |
console.log(log(['a','b'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This algorithm returns all the combinations of an array