An expression will be given which can contain open and close parentheses and optionally some characters, No other operator will be there in string. We need to remove minimum number of parentheses to make the input string valid. If more than one valid output are possible removing same number of parentheses then print all such output. Examples:
Input : str = “()())()” -
Output : ()()() (())()
There are two possible solutions
"()()()" and "(())()"
Input : str = (v)())()
Output : (v)()() (v())()
As we need to generate all possible output we will backtrack among all states by removing one opening or closing bracket and check if they are valid if invalid then add the removed bracket back and go for next state. We will use BFS for moving through states, use of BFS will assure removal of minimal number of brackets because we traverse into states level by level and each level corresponds to one extra bracket removal. Other than this BFS involve no recursion so overhead of passing parameters is also saved. Below code has a method isValidString to check validity of string, it counts open and closed parenthesis at each index ignoring non-parenthesis character. If at any instant count of close parenthesis becomes more than open then we return false else we keep update the count variable.
/* C/C++ program to remove invalid parenthesis */
// method checks if character is parenthesis(open
// or closed)
return((c == '(') || (c == ')'));
// method returns true if string contains valid
intcnt = 0;
for(inti = 0; i < str.length(); i++)
if(str[i] == '(')
elseif(str[i] == ')')
if(cnt < 0)
return(cnt == 0);
// method to remove invalid parenthesis
// visit set to ignore already visited string
// queue to maintain BFS
// pushing given string as starting node into queue
The time and space complexity of the given program are as follows:
Time complexity: The program uses a BFS (breadth-first search) approach to traverse all possible combinations of removing invalid parentheses from the input string. The worst-case time complexity of BFS is exponential, O(b^d), where b is the branching factor (the average number of child nodes per node) and d is the depth of the search tree. In the given program, the branching factor is at most 2 (either remove or keep a parenthesis at each position), and the depth can be up to the length of the input string. Therefore, the worst-case time complexity of the program is O(2^n), where n is the length of the input string.
Space complexity: The program uses a queue to store the intermediate strings during the BFS traversal. The maximum size of the queue can be at most O(2^n), as there can be up to 2^n valid combinations of parentheses. Additionally, the program uses an unordered set to keep track of the visited strings and avoid duplicates. The size of the set can also be up to O(2^n), as each valid combination can potentially be a unique string. Therefore, the space complexity of the program is O(2^n).
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...