# How to remove all 2 consecutives chars in a string

I have the string `s = "Mississipi"`, and I’d like to remove all 2-consecutives characters in this string so that only `"Mpi"` is returned. How to do that?

I think a potentially tricky bit to this is related to whether or not strings are mutable (look this up). This leads to a narrower set of logical solutions.

Once you know whether it’s mutable or not, it’s a matter of working out what the logic would be to get `"Mpi"`. Think: how would you tell a stranger what you want without giving them what the final result looks like?

2 Likes

OK. I think I understood what you meant. Actually, I have a string that I want to remove all 2-consecutives characters.
For example, I have a string `s = "Mississipi"`. It should do the following sequences:

`"Mississipi" -> "Miiipi" (because the 2 consecutives 'ss' have been removed) -> "Mipi (because the the 2-consecutives 'ii' have been removed)"`

So the final result should returned the string `"Mipi"`.

Close, you’re example would be to actually mutate the string (but strings are immutable in python).

So the key point is that the `result` which you are returning is not the original string with mutations applied recursively (if I’m reading the original prompt correctly, do you have a link for reference?). You basically should look at your initial string, if it has any consecutive characters, those don’t go in your `result` string.

Example `"apple"` would return `"ale"`.
“ttxxtt” would return `""`.
If you have `"atta"` it should return the single `"a"`.

Thinking about this in sets is useful and may even lead you to find some particular ways to solve it in python (there are a few ways). What does it mean to be in a set? Can a set have repeated instances of one element? etc.

Another edge case is what happens when you have `"ttxxxtt"`, usually either the problem states it somewhere or you have to test for yourself. (Should it return `""` or `"x"`? This will vary on the wording of the problem prompt)

3 Likes

I think I undestood how I should do that. Thanks for your reply

1 Like