Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c665a58

Browse files
committed
Have implemented the program to add the parentheses to make the valid parentheses.
1 parent c8e192e commit c665a58

File tree

2 files changed

+80
-33
lines changed

2 files changed

+80
-33
lines changed

.idea/workspace.xml

Lines changed: 32 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Minimum Add to Make Parentheses Valid
3+
Link: https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
4+
5+
A parentheses string is valid if and only if:
6+
7+
It is the empty string,
8+
It can be written as AB (A concatenated with B), where A and B are valid strings, or
9+
It can be written as (A), where A is a valid string.
10+
You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.
11+
12+
For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))".
13+
Return the minimum number of moves required to make s valid.
14+
15+
Example 1:
16+
Input: s = "())"
17+
Output: 1
18+
19+
Example 2:
20+
Input: s = "((("
21+
Output: 3
22+
23+
Constraints:
24+
1 <= s.length <= 1000
25+
s[i] is either '(' or ')'.
26+
*/
27+
package com.raj;
28+
29+
public class MinimumAddToMakeParenthesesValid {
30+
public static void main(String[] args) {
31+
// Initialization.
32+
String s = "())";
33+
int opening = 0;
34+
int closing = 0;
35+
36+
// Logic.
37+
for (char c : s.toCharArray()) {
38+
if (c == '(') {
39+
opening++;
40+
} else {
41+
closing++;
42+
}
43+
}
44+
45+
// Display the result.
46+
System.out.println("The parentheses required to make the valid parentheses is: " + Math.abs(opening - closing));
47+
}
48+
}

0 commit comments

Comments
 (0)