Here, we go for a recursive solution.
Our function basically takes an every element (except the last one) of the string, and compares it to the one next to it.
If they are equal, delete both of them from the string
We are not done yet. We have only made one pass. This may generate a new string which may be further reducible. So we introduce a boolean variable "done" initialized as True right at the beginning of the function, before any other statement.
Whenever we find two adjacent elements being equal, we set the value of done = False. At the end of the function, we check if done is False. If it is false, we set
string = superReducedString(string)
(recursion)
When the super reduced string is obtained, "done" will remain true. When done is true, return the string.
Our function basically takes an every element (except the last one) of the string, and compares it to the one next to it.
If they are equal, delete both of them from the string
We are not done yet. We have only made one pass. This may generate a new string which may be further reducible. So we introduce a boolean variable "done" initialized as True right at the beginning of the function, before any other statement.
Whenever we find two adjacent elements being equal, we set the value of done = False. At the end of the function, we check if done is False. If it is false, we set
string = superReducedString(string)
(recursion)
When the super reduced string is obtained, "done" will remain true. When done is true, return the string.
No comments:
Post a Comment