Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class GeneratePaqrenthesis {
- static void solve(ArrayList<String> output, int open, int close, String ans){
- if(open == 0 && close == 0){
- output.add(ans);
- return;
- }
- if(open>0){
- ans += "(";
- solve(output,open - 1, close, ans);
- ans.replace(String.valueOf(ans.charAt(ans.length()-1)),"");
- }
- if(close>open){
- ans += ")";
- solve(output,open , close-1, ans);
- ans.replace(String.valueOf(ans.charAt(ans.length()-1)),"");
- }
- }
- public static void main(String[] args) {
- int open = 2;
- int close = 2;
- String ans="";
- ArrayList<String> output = new ArrayList<>();
- solve(output,open,close,ans);
- for(String s:output){
- System.out.println(s);
- }
- }
- }
- //here the output is
- //(()()
- //(()) this is wrong with one extra ( braces
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement