55// Problem Title => Remove Invalid Parenthesis
66public class RemoveInvalidParenthesis {
77
8- // check if character parenthesis (is open or close)
9- static boolean isParenthesis (char c ){
10- return ((c == '(' ) || (c == ')' ));
8+ // check if character parenthesis (is open or close)
9+ static boolean isParenthesis (char c ) {
10+ return ((c == '(' ) || (c == ')' ));
11+ }
12+
13+ static boolean isValidString (String str ) {
14+ int count = 0 ;
15+ for (int i = 0 ; i < str .length (); i ++) {
16+ if (str .charAt (i ) == '(' )
17+ count ++;
18+ else if (str .charAt (i ) == ')' )
19+ count --;
20+ if (count < 0 )
21+ return false ;
1122 }
23+ return (count == 0 );
24+ }
25+
26+ // Method to remove invalid parenthesis (without using HashSet)
27+ static void removeInvalidParenthesis (String str ) {
28+ if (str .isEmpty ()) return ;
29+
30+ Queue <String > q = new LinkedList <>();
31+ String temp ;
32+
33+ // Pushing given string as starting node into queue
34+ q .add (str );
35+
36+ int level = 0 ; // To track level for processing only valid strings of current level
37+ boolean foundValid = false ; // Flag to indicate if a valid string was found
38+
39+ while (!q .isEmpty ()) {
40+ int size = q .size (); // Process all strings of current level
41+
42+ for (int i = 0 ; i < size ; i ++) {
43+ str = q .poll ();
1244
13- static boolean isValidString (String str ){
14- int count = 0 ;
15- for (int i = 0 ; i < str .length (); i ++){
16- if (str .charAt (i ) == '(' )
17- count ++;
18- else if (str .charAt (i ) == ')' )
19- count --;
20- if (count < 0 )
21- return false ;
45+ if (isValidString (str )) {
46+ System .out .println (str );
47+ foundValid = true ;
2248 }
23- return (count == 0 );
24- }
2549
26- // Method to remove invalid parenthesis
27- static void removeInvalidParenthesis (String str ){
28- if (str .isEmpty ())
29- return ;
30-
31- // visit set to ignore already visited string
32- HashSet <String > visit = new HashSet <>();
33-
34- // queue to maintain BFS
35- Queue <String > q = new LinkedList <>();String temp ;
36- boolean level = false ;
37-
38- // pushing given string as
39- // starting node into queue
40- q .add (str );
41- visit .add (str );
42- while (!q .isEmpty ())
43- {
44- str = q .peek (); q .remove ();
45- if (isValidString (str ))
46- {
47- System .out .println (str );
48-
49- // If answer is found, make level true
50- // so that valid string of only that level
51- // are processed.
52- level = true ;
53- }
54- if (level )
55- continue ;
56- for (int i = 0 ; i < str .length (); i ++)
57- {
58- if (!isParenthesis (str .charAt (i )))
59- continue ;
60-
61- // Removing parenthesis from str and
62- // pushing into queue,if not visited already
63- temp = str .substring (0 , i ) + str .substring (i + 1 );
64- if (!visit .contains (temp )) {
65- q .add (temp );
66- visit .add (temp );
67- }
68- }
50+ // If a valid string is found at the current level, skip processing further strings
51+ // at this level (optimization)
52+ if (foundValid && level > 0 ) {
53+ continue ;
6954 }
70- }
7155
72- public static void main (String [] args ) {
73- Scanner sc = new Scanner (System .in );
74- String expression = sc .nextLine ();
75- removeInvalidParenthesis (expression );
56+ for (int j = 0 ; j < str .length (); j ++) {
57+ if (!isParenthesis (str .charAt (j ))) continue ;
58+
59+ // Removing parenthesis from str and pushing into queue
60+ temp = str .substring (0 , j ) + str .substring (j + 1 );
61+ q .add (temp );
62+ }
63+ }
7664
77- expression = "()v)" ;
78- removeInvalidParenthesis ( expression );
65+ level ++; // Move to next level (indicates processing children of current level strings)
66+ foundValid = false ; // Reset flag for next level
7967 }
80- }
68+ }
69+
70+ public static void main (String [] args ) {
71+ Scanner sc = new Scanner (System .in );
72+ String expression = sc .nextLine ();
73+ removeInvalidParenthesis (expression );
74+ sc .close ();
75+
76+ expression = "()v)" ;
77+ removeInvalidParenthesis (expression );
78+ }
79+ }
0 commit comments