DEV Community

loading...

Discussion on: Daily Challenge #91 - Bananas

Collapse
erezwanderman profile image
erezwanderman

Typescript:

const solve = (input: string, target: string = 'banana') => {
  if (input === '' && target === '') return [''];
  if (input === '') return [];

  if (input[0] !== target[0]) {
    return solve(input.substring(1), target).map(x => '-' + x);
  }
  return [
    ...solve(input.substring(1), target.substring(1)).map(x => input[0] + x),
    ...solve(input.substring(1), target).map(x => '-' + x)
  ];
}

console.log(solve('bbananana'));