-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path24thJune(II).java
More file actions
29 lines (26 loc) · 809 Bytes
/
Copy path24thJune(II).java
File metadata and controls
29 lines (26 loc) · 809 Bytes
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
import java.util.List;
import java.util.ArrayList;
public class GeneratePraa {
static List<String> generateParenthesis(int n) {
List<String> ans = new ArrayList<>();
helper(ans, "", 0, 0, n);
return ans;
}
static void helper(List<String> ans, String currentBracket, int open , int close, int max){
if(currentBracket.length()==max*2){
ans.add(currentBracket);
return;
}
if(open<max){
helper(ans,currentBracket+ "(", open+1, close, max);
}
if(close<open){
helper(ans,currentBracket+ ")", open, close+1, max);
}
}
public static void main(String[] args) {
System.out.println(generateParenthesis(3));
}
}
// OP ---
// [((())), (()()), (())(), ()(()), ()()()]