public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
solve(2 * n, "");
}
public static void solve(int n, String ans) {
if (ans.length() > n) {
return;
}
if (ans.length() == n) {
if (isBalanced(ans)) {
System.out.println(ans);
} else {
return;
}
}
solve(n, '(' + ans);
solve(n, ')' + ans);
return;
}
public static boolean isBalanced(String s) {
Stack<Character> stack = new Stack<>();
boolean balanced = true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
stack.push(s.charAt(i));
} else if (s.charAt(i) == ')') {
if (stack.isEmpty()) {
balanced = false;
break;
} else {
if (s.charAt(i) == ')' && stack.peek() == '(') {
stack.pop();
} else {
balanced = false;
}
}
}
}
if (balanced && stack.isEmpty()) {
return true;
} else {
return false;
}
}
Wrong answer Generate Parentheses
for input 3 your code gives
()()()
(())()
()(())
(()())
((()))
correct answer should be
()()()
()(())
(())()
(()())
((()))
debug for this