- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if it is possible to obtain a Balanced Parenthesis by shifting brackets to either end at most K times
In this problem, we need to check whether we can get the valid balanced subsequence of the parenthesis by moving at most K characters of the string at the end.
To solve the problem, we can use the stack data structure. The logic to solve the problem is that if we find more than K ‘)’ (closing parenthesis) before the ‘(‘ (opening parenthesis), we can’t make a string into a valid subsequence.
Problem statement – We have given string str containing the ‘(‘ and ‘)’ sequence of parenthesis. The length of the string is N. Also, We have given a positive integer K. We need to check whether we can make the given string to valid parenthesis subsequence by moving at most K characters of the string at the end of the string. Print ‘Yes’ or ‘No’ based on whether we can make a valid subsequence.
Sample examples
Input– str = ")()()(", k = 1
Output– ‘Yes’
Explanation– After moving the first character at the end of the string, we can get ‘()()()’, which is a valid parenthesis subsequence
Input– str = "))())()(((", k = 2
Output– No,
Explanation– We can move a maximum of 2 characters at the end of the string. So, After moving the first 2 characters at the end of the string, we can get ‘())()((())’, which is not a valid parenthesis subsequence.
Input– str = ‘()(‘, k = 10
Output– No
Explanation– The string doesn’t contain the same number of opening and closing brackets, so creating a valid parenthesis subsequence is impossible.
Approach 1
We will use the stack data structure to solve the problem in this approach.
Algorithm
If n is odd, return false as we can’t get a subsequence of balanced parenthesis if the string length is odd.
Define the ‘open’ and ‘close’ variables and initialize with zero.
Use the loop to traverse the string. If the current character is ‘(‘, increase the value of open by 1. Otherwise, increase the value of ‘)’ by 1.
If open and closed are not equal, return false, as we can’t get a balanced subsequence if the total number of opening and closing braces are not equal.
Initialize the ‘res’ variable with zero and define an empty stack.
Start traversing the string again.
If the current character is ‘(‘, push it to the stack.
If the current character is ‘)’, and the stack is not empty, remove the top element from the stack. Otherwise, increase the value of ‘res’ by 1 as we need to move the character at the end.
Finally, check if the value of ‘res’ is less than K. If yes, return true. Otherwise, return false.
Example
#include <bits/stdc++.h> using namespace std; // function to check if it is possible to make the string balanced according to the given condition bool getMinMoves(string s, int n, int k) { // If the length of the string is odd, it is not possible to make the string balanced if(n % 2 != 0) { return false; } // count the total number of opening and closing brackets int open = 0, close = 0; // Traverse the string for (int i = 0; i < n; i++) { // If the current character is opening a bracket, increment open. Else increment close if (s[i] == '(') { open++; } else { close++; } } // If the number of opening brackets is not equal to the number of closing brackets, it is not possible to make string balanced if (open != close) { return false; } // to store moves required to make string balanced int res = 0; // Defining the stack stack<char> st; // Traverse the string for (int i = 0; i < n; ++i) { // If the current character is opening the bracket, push to the stack if (s[i] == '(') { st.push(s[i]); } else { if(!st.empty()) { st.pop(); } else { // Increment the res ++res; } } } // If res is less than or equal to k, then return true. Else return false if (res <= k) { return true; } else { return false; } } int main() { string str = "))())()((("; int K = 2; if (getMinMoves(str, str.length(), K)) { cout << "Yes, It is possible to make valid parenthesis by moving characters to either end at most K number of times"; } else { cout << "No, It is not possible to make valid parenthesis by moving characters to either end at most K number of times"; } return 0; }
Output
No, It is not possible to make valid parenthesis by moving characters to either end at most K number of times
Time complexity – O(N) as we traverse the string.
Space complexity – O(N), as we use the stack.
Approach 2
In this approach, we will write optimized code without using the stack to improve the space complexity of the code.
Algorithm
If the string length is odd, return false.
Use the count() method to count the total number of ‘(‘ and ‘)’ characters. If the count of both is not equal, return false.
Define the ‘res’ and ‘count’ variables and initialize with zero.
Start traversing the string
If the current character is ‘(‘, increase the value of the count by 1. Else, decrease the value of the count by 1.
If the value of ‘count’ is less than zero, increase the value of ‘res’ by 1, and update the count to zero.
Once the loop iteration completes, return the ‘res <= k’ value.
Example
#include <bits/stdc++.h> using namespace std; bool getMinMoves(string s, int n, int k) { // If the length of the string is odd, it is not possible to make the string balanced if(n % 2 != 0) { return false; } int open = 0, close = 0; // count the number of opening and closing brackets open = count(s.begin(), s.end(), '('); close = count(s.begin(), s.end(), ')'); // If the number of opening brackets is not equal to the number of closing brackets, it is not possible to make string balanced if (open != close) { return false; } // to store moves required to make string balanced int res = 0; int count = 0; // Traverse the string for (int i = 0; i < n; ++i) { // If the current character is opening bracket, increment count by 1 if (s[i] == '(') { ++count; } else { // Decrement count by 1 --count; // If the count becomes negative, then update the count to 0 and increment res by 1. if (count < 0) { // Update the count count = 0; // Increment the res ++res; } } } return (res <= k); } int main() { string str = ")()()("; int K = 1; if (getMinMoves(str, str.length(), K)) { cout << "Yes, It is possible to make valid parenthesis by moving characters to either end at most K number of times"; } else { cout << "No, It is not possible to make valid parenthesis by moving characters to either end at most K number of times"; } return 0; }
Output
Yes, It is possible to make valid parenthesis by moving characters to either end at most K number of times
Time complexity – O(N) as we traverse the string.
Space complexity – O(1), as we don’t use dynamic space.
To Continue Learning Please Login
Login with Google