DEV Community

dev.to staff
dev.to staff

Posted on

7

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!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

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.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay