You’re given a non-empty string made in its entirety from opening and closing braces. Your task is to find the minimum number of “operations” needed to make the string stable. The definition of being stable is as follows:
The only operation allowed on the string is to replace an opening brace with a closing brace, or visa-versa.
Solution Heuristics: Traverse the string. When the number of opening braces( { ) matches the number of closing braces ( } ), the string is balanced. Else it's not balanced. So count both of them. Make a list. Insert only opening braces. If a closing brace is found delete one brace( if the size of the list is > 0 ) from the list. Because they would make a pair. If the list is empty then we'll convert the just found closing brace to opening brace and insert into the list. This will count as a move. When the iteration is finished, we will reverse half of the braces in the list and they will also count as moves. Now, find out the total summation of the moves for result.
- An empty string is stable.
- If S is stable, then {S} is also stable.
- If S and T are both stable, then ST (the concatenation of the two) is also stable.
The only operation allowed on the string is to replace an opening brace with a closing brace, or visa-versa.
Solution Heuristics: Traverse the string. When the number of opening braces( { ) matches the number of closing braces ( } ), the string is balanced. Else it's not balanced. So count both of them. Make a list. Insert only opening braces. If a closing brace is found delete one brace( if the size of the list is > 0 ) from the list. Because they would make a pair. If the list is empty then we'll convert the just found closing brace to opening brace and insert into the list. This will count as a move. When the iteration is finished, we will reverse half of the braces in the list and they will also count as moves. Now, find out the total summation of the moves for result.
Practice Problem: SPOJ ANARC09A - Seinfeld
Comments
Post a Comment