String game exercise - I have no idea what to do

This is my first post on the forum. :grinning:

I have trouble solving this exercise:
Two kids are playing a game. One kid makes up up a string s from small letters only and then removes all letters ‘a’ from it - string t. Next: merges both strings together into one s+t string. I.e. ‘car’ —> ‘carcr’. Then shows the string s+t to the second kid that is supposed to guess the original string s. Sometimes first kid tries to fool the other and string t is not just string s without letters ‘a’. I.e. when the second kid sees string ‘carbm’ he is sure that the first one is trying to trick him.
Write a programme that checks if the string given was made by merging strings s and t, where string t is equal to string s with all letters ‘a’ removed.

I.e. input: ‘carcr’, output: ‘car’
input: ‘dada’, output: ‘impossible’
input: ‘aaaaa’, output: ‘aaaaa’

I have absolutely no idea how I should solve this exercise. I think I could deal with the first case by counting consonants and then slice the string somewhere (?), but those ‘fooling tries’ run me crazy.

I would be thankful if anyone could help me with this exercise using either Python or C++.

Thanks in advance,

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

My code:

test = "carcr"
a_occurances = test.count('a')
x = len(test) - a_occurances
n = a_occurances + x//2

It works perfectly with ‘car’ case and ‘aaaaa’ case but it fails to do the thing with ‘dada’ case. That’s my problem and thanks for reply @iahleen

One possible solution would be to take the resulting string (test[0:n]), create a new string using the rules and see if it is equal to the input string.

1 Like

I don’t understand what you mean. Test[0:n] for ‘dada’ returns 'dad. Not equal, not coreect. Whereas ‘aaaaa’ returns desired output ‘aaaaa’

You take resulting string and apply the rules:
‘dad’ + ‘dd’ == ‘daddd’ (not equal to input)
‘aaaaa’ + ‘’ == ‘aaaaa’ (good)
‘car’ + ‘cr’ == ‘carcr’ (good)

1 Like

Oh, I see. Thank you very much man. Now I see how trivial it was.