-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathGenerateParanthesis.java
More file actions
45 lines (36 loc) · 1 KB
/
GenerateParanthesis.java
File metadata and controls
45 lines (36 loc) · 1 KB
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
45
package Backtracking;
import java.util.ArrayList;
/**
* Author - archit.s
* Date - 27/10/18
* Time - 4:14 PM
*/
public class GenerateParanthesis {
public void helper(ArrayList<String> r, int left, int right, StringBuilder temp){
if(left>right){
return;
}
else if(left ==0 && right == 0){
r.add(new String(temp));
return;
}
for(int i=0;i<2;i++){
if(i==0 && left!=0){
temp.append('(');
helper(r,left-1,right,temp);
temp.deleteCharAt(temp.length()-1);
}
else if(i == 1 && right !=0 ){
temp.append(')');
helper(r,left,right-1,temp);
temp.deleteCharAt(temp.length()-1);
}
}
}
public ArrayList<String> generateParenthesis(int A) {
ArrayList<String> r = new ArrayList<>();
StringBuilder s = new StringBuilder();
helper(r,A,A,s);
return r;
}
}