Skip to content

Instantly share code, notes, and snippets.

@casprwang
Created September 20, 2017 05:20
Show Gist options
  • Save casprwang/12b3f13dd45a90453f3e06abc81b90ca to your computer and use it in GitHub Desktop.
Save casprwang/12b3f13dd45a90453f3e06abc81b90ca to your computer and use it in GitHub Desktop.
// 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']))
@casprwang
Copy link
Author

This algorithm returns all the combinations of an array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment