DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #91 - Bananas

Given a string of letters a, b, n how many different ways can you make the word "banana" by crossing out various letters and then reading left-to-right?

(Use - to indicate a crossed-out letter)

Example

Input
bbananana

Output
b-anana--
b-anan--a
b-ana--na
b-an--ana
b-a--nana
b---anana
-banana--
-banan--a
-bana--na
-ban--ana
-ba--nana
-b--anana


This challenge comes from dinglemouse on CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (4)

Collapse
 
takakd profile image
Takahiro Kudo

Umm... I feel there is another good solution🤔

Solution using Python3

def solve(s, word):
    ret = []

    if word == '':
        ret.append(''.rjust(len(s), '-'))
        return ret

    left_s = ''
    for si in range(len(s)):
        if word[0] == s[si]:
            left_s = ''.rjust(si, '-') + s[si]
            if s[si+1:] == '' and word[1:] == '':
                ret.append(left_s)
            else:
                right_s_list = solve(s[si+1:], word[1:])
                for right_s in right_s_list:
                    ret.append(left_s + right_s)
    return ret 

if __name__ == '__main__':
    for s in solve('bbananana', 'banana'):
        print(s)
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'));
Collapse
 
schnipdip profile image
schnipdip

Is the input wrong? Are there supposed to be two b's?

Collapse
 
thepeoplesbourgeois profile image
Josh

Yes.