{"id":"1f4b9f08-0c0e-47da-b63c-1cb379e5dd78","name":"Minimum Remove To Make Valid Parentheses","description":"1: Given a string s of '(' , ')' and lowercase English characters\r\n2: Your task is to remove the minimum number of parentheses ( '(' or ')') so that the resulting parentheses string is valid and return it.\r\n3: In case of multiple valid strings give precedence in keeping innermost parenthesis.\r\n\r\nexample\r\n(a(b(c)d) this string has (a(bc)d), (ab(c)d) and a(b(c)d) 3 valid strings.\r\nAmong all 3 valid strings a(b(c)d) has the innermost parentheses.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1: 1 &lt;= s.length &lt;= 10^5\r\n2: s[i] is one of '(' , ')' and lowercase English letters.","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\n string removeParentheses(string s)\n{ \n // write your code here\n}\nint main(){ \n string s;\n cin>>s;\n string result = removeParentheses(s);\n cout<< result<<endl;\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static String reverseParentheses(String s) {\r\n return null;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n String result = reverseParentheses(read.readLine());\r\n System.out.println(result);\r\n \r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"a)b(c)d","sampleOutput":"ab(c)d\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"e1dee779-1940-469f-95d0-642a651080be","name":"Minimum Remove To Make Valid Parentheses","slug":"minimum-remove-to-make-valid-parentheses","type":1}],"next":{"id":"9293eb7a-648b-46ee-bccb-859962b21e88","name":"Minimum Remove To Make Valid Parentheses Medium MCQ","type":0,"slug":"minimum-remove-to-make-valid-parentheses-medium-mcq"},"prev":{"id":"ccaf667e-acde-41ea-b208-2541d023f35b","name":"Reverse Substrings Between Each Pair Of Parentheses Medium","type":3,"slug":"reverse-substrings-between-each-pair-of-parentheses-medium"}}}

Minimum Remove To Make Valid Parentheses

1: Given a string s of '(' , ')' and lowercase English characters 2: Your task is to remove the minimum number of parentheses ( '(' or ')') so that the resulting parentheses string is valid and return it. 3: In case of multiple valid strings give precedence in keeping innermost parenthesis. example (a(b(c)d) this string has (a(bc)d), (ab(c)d) and a(b(c)d) 3 valid strings. Among all 3 valid strings a(b(c)d) has the innermost parentheses.

{"id":"1f4b9f08-0c0e-47da-b63c-1cb379e5dd78","name":"Minimum Remove To Make Valid Parentheses","description":"1: Given a string s of '(' , ')' and lowercase English characters\r\n2: Your task is to remove the minimum number of parentheses ( '(' or ')') so that the resulting parentheses string is valid and return it.\r\n3: In case of multiple valid strings give precedence in keeping innermost parenthesis.\r\n\r\nexample\r\n(a(b(c)d) this string has (a(bc)d), (ab(c)d) and a(b(c)d) 3 valid strings.\r\nAmong all 3 valid strings a(b(c)d) has the innermost parentheses.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1: 1 &lt;= s.length &lt;= 10^5\r\n2: s[i] is one of '(' , ')' and lowercase English letters.","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\n string removeParentheses(string s)\n{ \n // write your code here\n}\nint main(){ \n string s;\n cin>>s;\n string result = removeParentheses(s);\n cout<< result<<endl;\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static String reverseParentheses(String s) {\r\n return null;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n String result = reverseParentheses(read.readLine());\r\n System.out.println(result);\r\n \r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"a)b(c)d","sampleOutput":"ab(c)d\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"e1dee779-1940-469f-95d0-642a651080be","name":"Minimum Remove To Make Valid Parentheses","slug":"minimum-remove-to-make-valid-parentheses","type":1}],"next":{"id":"9293eb7a-648b-46ee-bccb-859962b21e88","name":"Minimum Remove To Make Valid Parentheses Medium MCQ","type":0,"slug":"minimum-remove-to-make-valid-parentheses-medium-mcq"},"prev":{"id":"ccaf667e-acde-41ea-b208-2541d023f35b","name":"Reverse Substrings Between Each Pair Of Parentheses Medium","type":3,"slug":"reverse-substrings-between-each-pair-of-parentheses-medium"}}}
plane

Editor


Loading...

Minimum Remove To Make Valid Parentheses

medium

1: Given a string s of '(' , ')' and lowercase English characters 2: Your task is to remove the minimum number of parentheses ( '(' or ')') so that the resulting parentheses string is valid and return it. 3: In case of multiple valid strings give precedence in keeping innermost parenthesis. example (a(b(c)d) this string has (a(bc)d), (ab(c)d) and a(b(c)d) 3 valid strings. Among all 3 valid strings a(b(c)d) has the innermost parentheses.

Constraints

1: 1 <= s.length <= 10^5 2: s[i] is one of '(' , ')' and lowercase English letters.

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

a)b(c)d

Sample Output

ab(c)d

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode