-
Notifications
You must be signed in to change notification settings - Fork 231
/
GenerateParenthesis
44 lines (41 loc) · 1.23 KB
/
GenerateParenthesis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Driver code
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: "); // enter input in range 1 to 8
int input = sc.nextInt();
Solution obj = new Solution();
ArrayList<String> ans = obj.generateParenthesis(input);
System.out.print(ans);
}
}
// if open less than n add open
// if closed less than open add closed
// if open equals closed add string and return
class Solution {
ArrayList<String> al;
public void recur(int n, int open, int closed, StringBuilder s){
if(open == n && closed == n){
String ans = s.toString();
al.add(ans);
return;
}
if(open < n){
s.append('(');
recur(n,open+1,closed,s);
s.delete(s.length() - 1, s.length());
}
if(closed < open){
s.append(')');
recur(n,open,closed+1,s);
s.delete(s.length() - 1, s.length());
}
}
public ArrayList<String> generateParenthesis(int n) {
al = new ArrayList<>();
StringBuilder s = new StringBuilder();
recur(n,0,0,s);
return al;
}
}