Skip to content

Instantly share code, notes, and snippets.

@sw331
Created September 20, 2017 05:20
Show Gist options
  • Select an option

  • Save sw331/12b3f13dd45a90453f3e06abc81b90ca to your computer and use it in GitHub Desktop.

Select an option

Save sw331/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']))
@sw331
Copy link
Copy Markdown
Author

sw331 commented Sep 20, 2017

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