Understanding function to generate parentheses - java

I have this algorithm to generate all combinations of well-formed parentheses.
Can someone explain the core concept of the algorithm? I tried debugging through it, but I still can't seem to grasp the underlying concept behind the algorithm.
Additionally, any general advice for how one can come up with such an algorithm for this problem, i.e how did one even get so clever to solve it this way, or what practice one has to do to reach this stage.
Problem:
Given n pairs of parentheses, write a function to generate
all combinations of well-formed parentheses. For example, given n = 3, a solution set is:
“((()))”, “(()())”, “(())()”, “()(())”, “()()()”
Code:
public ArrayList<String> generateParenthesis(int n) {
ArrayList<String> solutions = new ArrayList<String>();
recursion(n, new String(), solutions);
return solutions;
}
private void recursion(int n, String str, ArrayList<String> sol) {
if(str.length() == 2 * n)
sol.add(str);
else {
int left = 0;
int right = 0;
for(int i = 0; i < str.length(); ++i) {
if(str.charAt(i) == '(')
left++;
if(str.charAt(i) == ')')
right++;
}
if(left == right)
recursion(n, str + "(", sol);
else if(right < left) {
if(left < n)
recursion(n, str + "(", sol);
recursion(n, str + ")", sol);
}
}
}

It helps me to see visually how the calls are being stacked. I added a parameter String depth to the call and printed out depth + str on each call, adding four spaces to each depth parameter for a new call. This gives us a good view of the call order.
Here's the code for it:
recursion(3, new String(), solutions, "");
//...
private static void recursion(int n, String str, ArrayList<String> sol, String depth) {
System.out.println(depth + str);
//...
if(left == right)
recursion(n, str + "(", sol, depth + " ");
else if(right < left) {
if(left < n)
recursion(n, str + "(", sol, depth + " ");
recursion(n, str + ")", sol, depth + " ");
}
And here's what it prints out:
(
((
(((
((()
((())
((()))
(()
(()(
(()()
(()())
(())
(())(
(())()
()
()(
()((
()(()
()(())
()()
()()(
()()()
Each level of recursion adds another indent to the output. If two outputs are at the same level of indentation, then they were both called from the same level of recursion.
Here's another visual:
Note that each node is a deeper level of recursion and each time a child node comes straight down out of a parent node, it doesn't split into two recursive paths. That is, the parent node only calls recursion once.

Recursion can definitely mess with your head. Here's another approach which might be easier to follow:
void generate() {
ArrayList<String> results = new ArrayList<String>();
generateParentheses(4, 0, new StringBuilder(), results);
System.out.println(results);
}
void generateParentheses(final int remaining, final int openCount, final StringBuilder s, final List<String> results) {
if (remaining == 0 && openCount == 0) {
results.add(s.toString());
return;
}
if (openCount > 0) { // we can close the open one
s.append(")");
generateParentheses(remaining, openCount-1, s, results);
s.setLength(s.length()-1); // pop the last char off
}
if (remaining > 0) { // start a new one
s.append("(");
generateParentheses(remaining-1, openCount+1, s, results);
s.setLength(s.length()-1); // pop the last char off
}
}
Output is [()()()(), ()()(()), ()(())(), ()(()()), ()((())), (())()(), (())(()), (()())(), (()()()), (()(())), ((()))(), ((())()), ((()())), (((())))]
This goes at the problem from the other end. How do you come up with these patterns?
Start with the number of pairs (remaining).
There are only two possibilities: open or closed. An open parentheses can only be appended if there are some remaining to append. A close parentheses can only be appended if there is a corresponding opening parentheses to close.
So you just need to keep a count of how many remaining you have, and how deep into parentheses you are. Let recursion handle the rest.

Here's my attempted breakdown of the provided algorithm:
if(str.length() == 2 * n)
sol.add(str);
You know you're done if the string length is twice the number of pairs of parentheses. Why?
Because each pair of parentheses is 2 characters long, having n pairs of parentheses means you have 2 * n characters total (i.e. length / 2 == number of parentheses)
int left = 0;
int right = 0;
for(int i = 0; i < str.length(); ++i) {
if(str.charAt(i) == '(')
left++;
if(str.charAt(i) == ')')
right++;
}
This loops through the string character by character and does some tests and changes left and right depending on whether the particular character is a opening or closing parentheses. Based on that, can you figure out what left and right are at the end of the loop?
left and right are the number of opening/closing parentheses in the string, respectively
if(left == right)
recursion(n, str + "(", sol);
else if(right < left) {
if(left < n)
recursion(n, str + "(", sol);
recursion(n, str + ")", sol);
}
If left == right, then you call the same function, except you tack on an opening parenthesis onto the existing string. Why add a parenthesis, and why is it an opening parenthesis?
You know you're not done, because if you were you would have triggered the first if statement. Because adding a closed parenthesis would result in ill-formed parenthesis because there is no unclosed opening parenthesis (remember, left == right), the next logical step is to add an opening parenthesis.
If right < left, you know that you have at least one unclosed opening parenthesis. So you want to do another check.
if(left < n)
recursion(n, str + "(", sol)
Why do this check? If left < n, then you know that if you close all the open parentheses you won't have enough pairs (n pairs of parentheses have n opening parentheses). So might as well add another opening parenthesis!
The last statement has an implicit else associated with it. If left is not < n, then you know adding another left parenthesis would put you over the requested number of pairs. So add a closing parenthesis and continue.

The core concept:
1)X:If a string has more closing parentheses than opening ones, adding more parentheses to the right won't make it a well-formed combination.
2)All well-formed combinations have equal number of opening and closing parentheses. Hence, each type can be found exactly n times
3)If the number of closing ones is less than the number of the opening ones, then we can always make a consequence well-formed, by adding more closing parentheses.
This algorithm builds the combination, adding new symbols to the right
public ArrayList<String> generateParenthesis(int n) {
ArrayList<String> solutions = new ArrayList<String>();
recursion(n, new String(), solutions);
return solutions;
}
private void recursion(int n, String str, ArrayList<String> sol) {
//If we got a sting long enough, we return it. This means a) We generate all
//strings only once. b)If a string of length 2*n is created, then it is correct. Other
//code should satisfy these conditions
if(str.length() == 2 * n)
sol.add(str);
else {
int left = 0;
int right = 0;
for(int i = 0; i < str.length(); ++i) {
if(str.charAt(i) == '(')
left++;
if(str.charAt(i) == ')')
right++;
}
//left and right are now numbers of parentheses in the string.
//Opening and closing respectively.
if(left == right)//On each step we maintain the condition X
//that the number of closing brackets is less or equal to the number of opening.
//Therefore, is their numbers are equal we can only add opening ones
recursion(n, str + "(", sol);
else if(right < left) { // if X is maintained and we can add
//both types
if(left < n)// The number of opened should be no more than n,
//according to 2)
recursion(n, str + "(", sol);
recursion(n, str + ")", sol);//The number of closing ones is
//limited by X by the number of opening ones, which is limited by n => the number of
//the closed ones is limited by n => we can add them as long as it doesn't violate X
}
}
}

I wrote my own recursive parentheses generator in a different style. It basically builds up a String, but at each recursive call, a new String is created so that the backtracking is correct. I hope someone finds it helpful.
import java.util.ArrayList;
import java.util.List;
public class GenerateParentheses {
// N: The max number of matching parentheses. This value does not change.
// usedL, usedR : Number of left and right parentheses already used in 'current' string.
// current: the current string being built.
// depth: recursion depth, used for pretty-printing
public static void generate(int N, int usedL, int usedR, String current, List<String> result, int depth) {
System.out.printf("%susedL=%d, usedR=%d, current='%s'\n",
getIndentation(depth), usedL, usedR, current);
if (usedL == N && usedR == N) {
// We've used up all the available parentheses (up to N),
// so add the current built string to the result.
result.add(current);
return;
}
if (usedL < N) {
// Add another left parenthesis "(".
String newCurrent = current + "(";
generate(N, usedL + 1, usedR, newCurrent, result, depth+1);
}
if (usedR < N && usedL > usedR) {
// Add another right parenthesis ")" if there are already
// used left parentheses.
String newCurrent = current + ")";
generate(N, usedL, usedR + 1, newCurrent, result, depth+1);
}
}
// Utility function used for pretty-printing.
private static String getIndentation(int depth) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < depth; i++) {
sb.append(" ");
}
return sb.toString();
}
public static void main(String argv[]) {
int N = 3;
int usedL = 0;
int usedR = 0;
String current = "";
List<String> result = new ArrayList<String>();
int depth = 0;
generate(N, usedL, usedR, current, result, depth);
for (String s : result) {
System.out.printf("%s\n", s);
}
}
}
Here is the output:
usedL=0, usedR=0, current=''
usedL=1, usedR=0, current='('
usedL=2, usedR=0, current='(('
usedL=3, usedR=0, current='((('
usedL=3, usedR=1, current='((()'
usedL=3, usedR=2, current='((())'
usedL=3, usedR=3, current='((()))'
usedL=2, usedR=1, current='(()'
usedL=3, usedR=1, current='(()('
usedL=3, usedR=2, current='(()()'
usedL=3, usedR=3, current='(()())'
usedL=2, usedR=2, current='(())'
usedL=3, usedR=2, current='(())('
usedL=3, usedR=3, current='(())()'
usedL=1, usedR=1, current='()'
usedL=2, usedR=1, current='()('
usedL=3, usedR=1, current='()(('
usedL=3, usedR=2, current='()(()'
usedL=3, usedR=3, current='()(())'
usedL=2, usedR=2, current='()()'
usedL=3, usedR=2, current='()()('
usedL=3, usedR=3, current='()()()'
((()))
(()())
(())()
()(())
()()()

How you get to the solution will be easier to see as you get more experience with recursion.
Idea: well-formed parentheses are always going to start with a left paren, have an equal number of left & right, and when reading left to right the following will always be true left >= right
Therefore when making a recursive solution, we'll use a simple rule: prefer to open left paren and let the nature of the recursive function unwinding take care of the rest
private void recursion(int n, String str, ArrayList<String> sol) {
if(str.length() == 2 * n)
sol.add(str);
We have n pairs, add the solution & return
else {
int left = 0;
int right = 0;
for(int i = 0; i < str.length(); ++i) {
if(str.charAt(i) == '(')
left++;
if(str.charAt(i) == ')')
right++;
}
Count up the number of left & right parens
if(left == right)
recursion(n, str + "(", sol);
str is currently balanced & since we prefer left over right, add a left
else if(right < left) {
This could just be an else, right will never be > left. Either way, it means that we're currently unbalanced, there is at least 1 more left than right.
if(left < n)
recursion(n, str + "(", sol);
Check if you can add another left, again because prefer left over right
recursion(n, str + ")", sol);
}
}
Add our right paren. This will either close the paren added in the line above it, or if that wasn't executed, it will close an earlier left (remember that we're in this block because it's currently unbalanced)

Here's a more simpler and intuitive solution to your problem.
Again, this follows the recursion idea but it is easier to read and more efficient than the one you posted.
public void generateParantheses(int n){
helper(n,0,0,"");
}
public void helper(int n, int open, int close, String result){
if(result.length()==2*n) {
// easy enough to understand? one open and one close for each n?
System.out.println(result);
return;
}
if(open<n){
//start off with all n open parantheses
helper(n, open+1, close, result+"(" );
}
if(close<open){
// now check if we can add closing parantheses on top of open in this condition
helper(n, open, close+1, result+")");
}
}

public static void findParenthisis(String s , int left ,int right){
if(left==right && left == 0){
System.out.println(s);
}
if(left > 0){
findParenthisis(s+'(',left-1,right);
}
if(left < right){
findParenthisis(s + ')',left,right-1);
}
}

void generateParenthesis(int open, int close, int position, int n, char[] str) {
/*
* open = open parenthesis
* close = close parenthesis
* position = 2*n (length of combination of valid parenthesis
* n = pair of parenthesis
* Algorithm:
* 1.Check if position == 2*n -- Yes, print the str
* 2.check if open is less than n
* If true, add a open parenthesis into str and call the function recursively by
* incrementing open by 1 and position by 1
* 3.check if close < open
* If true , add a close parenthesis and call the function recursively by
* incrementing close by 1 and position by 1*/
if(position ==str.length) {
for(int i=0;i<str.length;i++) {
System.out.print(str[i]);
}
System.out.println();
return;
}
if(open < n) {
str[position] = '(';
generateParenthesis(open+1,close,position+1,n,str);
}if(close< open) {
str[position]=')';
generateParenthesis(open,close+1,position+1,n,str);
}
}

I think I found a very intuitive solution that gets inspiration from a similar problem. Imagine you have a grid of size n x n. You want to move from the left-down corner to the right-upper corner. Every move to the right can be interpreted as a ( and every move up as ). The simplest case would be a grid of 1x1. There are two ways to go: RU (right then up) and UR (up then right) the first one corresponds to () and the second one )(. The second case is invalid so we discard all the paths from the upper triangle of the grid. For n=4 :
The problem can be solved recursively. We should start from R=0, U=0. Once we get to R=n, U=n we are done and should add the partial_solution to the solutions. There are two cases:
When R<n we still can move right and increment R.
When U<R notice we can only go up as much as we stay under the diagonal. In this case we can still move up and increment U.
def move(R, U, n, solutions, partial_solution):
if R == n and U == n:
solutions.append(partial_solution)
return
if R < n:
move(R + 1, U, n, solutions, partial_solution + '(')
if U < R:
move(R, U + 1, n, solutions, partial_solution + ')')
solutions = []
n=4
move(0, 0, n, solutions, '')
print(solutions)
Additional:
Note that this formulation also helps us to understand the number of ways we can form the parenthesis. Going from R=0, U=0 to R=n, U=n requires a string of R's and U's with the property that we should have exactly n R's and n U's. For example for n=3:
RRRUUU, RRURUU, URURUR, URRRUU, ...
It's a combinatorial problem and the number of ways is combination(2n, n). But we have to consider removing the upper triangle. This leaves us with 1/(n+1) * combination(2n, n) which is the Catalan number of n.

`class Solution:
def generateParenthesis(self, n):
# Backtracking
result_list = list()
stack = list()
def create_parenthesis(left_count, right_count):
if left_count == n and right_count == n:
result_list.append("".join(stack))
if left_count < n:
print("Appending ( to stack")
stack.append("(")
before_spacing = (left_count + right_count) * "\t"
print(before_spacing + "".join(stack))
create_parenthesis(left_count+1, right_count)
print("Popping from left_count < n")
stack.pop()
if left_count > right_count:
print("Appending ) to stack.")
stack.append(")")
before_spacing = (left_count + right_count) * "\t"
print(before_spacing + "".join(stack))
create_parenthesis(left_count, right_count+1)
print("Popping from left_count > right_count")
stack.pop()
create_parenthesis(0, 0)
return result_list`
print(Solution().generateParenthesis(n=3))
Output:
output

class Solution:
def generateParenthesis(self, n: int) -> list[str]:
stack = []
res = []
def back_Move(openPar,closedPar):
if openPar == closedPar==n:
res.append("".join(stack))
return
if openPar<n:
stack.append("(")
back_Move(openPar+1,closedPar)
stack.pop()
if closedPar<openPar:
stack.append(")")
back_Move(openPar,closedPar+1)
stack.pop()
back_Move(0,0)
return res

Related

Recursive method to replace all occurrences of a value in a 2D array

I have created a recursive method that replaces all occurrences of an element in a two dimensional double array. The issue is that I cannot seem to get this working without encountering a stack overflow error. Could someone please look at my code below and show me how to fix this? I have tried setting this up several times over the past few days. Thank you. Note that my arrays are 2 x 3, so the first if means that if you are at column 1 row 2, you are at the end of the array, and in that case you are done searching.
private static int replaceAll(double number, double replacementTerm) {
int i = 0;
int j = 0;
double searchFor = number;
double replace = replacementTerm;
if (i == 1 && j == 2) {
System.out.println("Search complete!");
}
if (twoDimArray2[i][j] == searchFor) {
System.out.println("Replaced An Element!");
twoDimArray2[i][j] = replace;
System.out.println(twoDimArray2[i][j]);
j++;
return replaceAll(searchFor, replace);
}
if (j == twoDimArray2.length) {
i++;
return replaceAll(searchFor, replace);
} else {
j++;
return replaceAll(searchFor, replace);
}
}
i and j should be method parameters instead of local variables so changes to their values can be tracked. Try to move right and down recursively if it does not exceed the bounds of the array. Note that this is much less efficient that iteration with two layers of for loops, as it will check multiple positions in the array more than once; to mitigate this, one can use a visited array to store all positions previous visited so they will not be checked again. See the below code in action here.
private static void replaceAll(double number, double replacementTerm, int i, int j) {
double searchFor = number;
double replace = replacementTerm;
if (twoDimArray2[i][j] == searchFor) {
System.out.println("Replaced An Element!");
twoDimArray2[i][j] = replace;
System.out.println(twoDimArray2[i][j]);
}
if (i == twoDimArray2.length - 1 && j == twoDimArray2[0].length - 1) {
System.out.println("Reached the end!");
return;
}
if (i + 1 < twoDimArray2.length) {
replaceAll(number, replacementTerm, i + 1, j);
}
if (j + 1 < twoDimArray2[0].length) {
replaceAll(number, replacementTerm, i, j + 1);
}
}

Setting numbers from 1 to chosen number using recursion only

After about 7 hours in a row I really need some help , I need to return from recursion the amount of options that can be by setting numbers from 1 to chosen number(maximum number) , it's forbidden to use loops/arrays , only recursion , the numbers are all positive(more than 0) and goes only more positively , example : good one : {1,2} , bad one : {2,1}.
example :
n = 3 , max = 2
n : The numbers that should be in the row
, max : The maximum number that can be in the row.
{1,1,1}
{1,1,2}
{1,2,2}
{2,2,2}
from that example that should return 4 because there are 4 options of 3 numbers that their value is maximum 2.
another one:
n=2
max=3
{1,1}
{1,2}
{1,3}
{2,2}
{2,3}
{3,3}
from that example it should return 6 because there are 6 options.
Without prior knowledge, this would probably be a challenging question even for an experienced mathematician. It is the count of multisets, one of the fundamental building blocks in combinatorics. I'll explain my understanding of the idea for the recurrence relation in Wikipedia.
Typically k is used for the multiset cardinality (what your question refers to as n), while n is used as the cardinality of the set (not multiset) to choose from (the max in your question).
For f(n, k), the base cases are:
f(n, 0) = 1
one way to fill the empty multiset
And,
f(0, k) = 0
no ways to choose from an empty set
For the regular case, we consider the nth element (from the set of choices). We'd like to count all the combinations that include it and all those where it's missing. Counting all combinations without the nth element is easy: we have the same multiset counting function applied to k with one less choice:
f(n - 1, k)
Now to count the combinations that include at least one nth element, we imagine taking all the ways of choosing from n items (some of which will not include an nth element) but saving one place in each combination where we will place an nth element, so we end up with:
f(n, k - 1)
Putting it all together:
function f(n, k){
if (n == 0)
return 0;
if (k == 0)
return 1;
return f(n - 1, k) + f(n, k - 1);
}
console.log(f(2, 3));
console.log(f(3, 2));
Recursion can be hard to comprehend at first, but it is very clear to read once you get to know it. The downside is that recursion requires way more space than the basic for-loop (Space complexity of recursive function). For some problems it can be easier to first write the recursive version and afterwards write it as for-loop. Also, if space is not a problem, it helps to make your code clean (no for-loops!)
I made some basic recursion that gives the correct answer for at least the two examples you wrote down. It may be possible that I missed an edge case: maybe a good practise to write every function call and some (edgy) test cases.
public int recursiveWrapper(int n, int max) {
return recursive(n, max, 1, 1);
}
public int recursive(int n, int max, int lower, int current) {
// // for your convenience
// System.out.println("n:" + n + " max:" + max + " lowerbound:" + lower + " current:" + current);
// Base case
if (n <= 1 && lower == max) {
return 1;
}
// Recursive step
// Sequence complete, move to next column
if (current == max) {
// Make sure the lower bound does not go beyond the max. number
int updatedLower = (lower + 1 > max) ? lower : lower + 1;
return 1 + recursive(n - 1, max, updatedLower, updatedLower);
}
return 1 + recursive(n, max, lower, current + 1);
}
In short:
In the second example:
n=2
max=3
{1,1}
{1,2}
{1,3}
{2,2}
{2,3}
{3,3}
Note the pattern of the numbers that appears due to the rule that the numbers from left to right have to be equal or larger:
Second column: 1>2>3 > 2>3 > 3
First column: 1>1>1 > 2>2 > 3
The 'lower bound' parameter in the recursion is basically the lowest possible number the new 'sequence' can take (where each sequence is lower bound -> max number). The base case is then when the lower bound equals the upper bound and each column has done all it 'sequences'. Possibly not a very clear explanation - maybe it helps when you see what is printed out by the commented line in the code I copy pasted.
Note: Maybe it is possible to do the recursion with less parameters. Make sure to read a lot about recursion (for example wikipedia or your studybook?). Recursions makes it easier to find solutions and understand complex and abstract problems.
I have write some less efficient code due to time, try look at this, it will give you dir, i hope,
package com.exercise;
import java.util.Arrays;
public class Permutation {
public static void permutation(String str) {
permutation("", str);
}
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0)
System.out.println(prefix);
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
}
}
private static void permutationOnInt(String prefix, String str, int max) {
int n = str.length();
if (n == 0)
System.out.println(prefix);
else {
for (int i = 0; i <= n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
}
}
public static int[] return_Array(int length) {
int[] a = new int[length];
for (int i = 0; i < length; i++) {
a[i] = i + 1;
}
return a;
}
public static void main(String[] args) {
String fn = Arrays.toString(return_Array(3));
String apple = String.join(",", fn.replace("[", "").replace("]", "").replace(",", "").replaceAll("\\s+", ""));
permutationOnInt("", apple, 3);
}
}
After you get the result you can convert it back to the array.
Importent : This code is totally not optimized. I will post optimized later

Split String differently under different conditions

I'm trying the following: public static Element parse(String input) {
gets a String as an input and has to determine if the input either is a fraction or an operator.
This is one example:
sin(x) + (1/x) / x + 7
My code already checks if there is the same amount of opening and closing parenthesis, as well as if there always is a numerator and a denominator if there is a fraction submitted.
Now I've got a problems:
1. A new Fraction element should be created if there is a "/" present in the String that is not surrounded by parenthesis. In the example above, sin(x) + (1/x) would be the numerator and x + 7 would be the denominator. I don't really have any idea how to split the String according to this.
Final Output
Help would be appreciated.
Thanks!
First of all you should maybe put your String into a charArray which is easier to deal with at some point.
What I would do then is taking the charArray in a for loop which counts up or down based on the average lenght of your numberator and denominator. You count on the one hand the numbers of opened parenthesis and on the other hand you look for "/" and if parenthesis is 0 you count this as a numerator...
Ok now in Code.
public static Element parse(String s) {
// ...
char[] c = s.toCharArray();
int brackets = 0;
for (int i = 0; i < c.length(); i++) {
if (c[i] == '(') brackets++;
else if (c[i] == ')') brackets--;
else if (c[i] == '/' && brackets == 0) return new FractionElement();
}
// ...
return new OperatorElement();
}
Hope I was able to help you ;) Had a similar problem recently...
PS: If you want to split the numerator and the denominator into 2 substrings just do this:
//...
else if (c[i] == '/' && brackets == 0) {
String numerator = s.substring(0, i);
String denominator = s.substring(i+1, s.length);
}
//...

Generate balanced parentheses in java

The question is:
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
I used to solve this problem using string as following codes:
public class Solution {
public List<String> generateParenthesis(int n) {
ArrayList<String> result = new ArrayList<String>();
//StringBuilder s = new StringBuilder();
generate(n, 0, 0, "", result);
return result;
}
public void generate(int n, int left, int right, String s, ArrayList<String> result){
//left is num of '(' and right is num of ')'
if(left < right){
return;
}
if(left == n && right == n){
result.add(s);
return;
}
if(left == n){
//add ')' only.
generate(n, left, right + 1, s + ")", result);
return;
}
generate(n, left + 1, right, s + "(", result);
generate(n, left, right + 1, s + ")", result);
}
}
Now I want to solve this problem using StringBuilder, the code is like this:
import java.util.ArrayList;
public class generateParentheses {
public static ArrayList<String> generateParenthesis(int n) {
ArrayList<String> result = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
generate(n, 0, 0, result, sb);
return result;
}
public static void generate(int n, int left, int right, ArrayList<String> result,
StringBuilder sb) {
if (left < right) {
return;
}
if (left == n && right == n) {
result.add(sb.toString());
sb = new StringBuilder();
return;
}
if (left == n) {
generate(n, left, right + 1, result, sb.append(')'));
return;
}
generate(n, left + 1, right, result, sb.append('('));
//sb.delete(sb.length(), sb.length() + 1);
generate(n, left, right + 1, result, sb.append(')'));
//sb.delete(sb.length(), sb.length() + 1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(generateParenthesis(4));
}
}
The result is not what I want:
(((()))), (((()))))())), (((()))))())))()), (((()))))())))()))(), (((()))))())))()))()))(())), (((()))))())))()))()))(())))()).........
Is there anyone tell me what is the problem? Thank you very much.
You ware close. Your mistakes ware:
trying to reset sb instead of removing only its last character
way you want to reset sb:
by using sb = new StringBuilder(); you are reassigning sb which is local variable of current method, not variable of method which invoked it (Java is not pass-by-reference but pass-by-value).
your almost correct attempt ware commented sb.delete(sb.length(), sb.length() + 1); but here you are actually trying to remove characters starting from position sb.length(), but just like arrays indexes of character in StringBuilder are from 0 till sb.length() - 1 so is trying to remove one character after last character which effectively can't remove anything.
What you needed here is
sb.delete(sb.length() - 1, sb.length());
or more readable
sb.deleteCharAt(sb.length() - 1);
but probably best approach in terms of performance setLength (described at bottom of answer)
sb.setLength(sb.length() - 1);
your logic of when to remove characters from StringBuilder is also flawed
you are doing it only in one place which ends (backtracks) recursive calls: after finding correct results. But what about other cases like if (left < right) or most importantly, if method will end normally like
generate(3, 1, 1, ')');
generate(3, 1, 2, ')');//here we stop and backtrack
Here generate(3, 1, 2, ')'); ends and removes last character from sb, but shouldn't previous method generate(3, 1, 1, ')') also remove its own ) added to StringBuilder?
In other words you shouldn't remove last character only at end of successful condition in recursive call, but after each recursive call, to make sure that method will also remove character it adds.
So change your code to something like
public static void generate(int n, int left, int right, ArrayList<String> result,
StringBuilder sb) {
if (left < right) {
return;
}
if (left == n && right == n) {
result.add(sb.toString());
return;
}
if (left == n) {
generate(n, left, right + 1, result, sb.append(')'));
sb.deleteCharAt(sb.length() - 1);// <--
return;
}
generate(n, left + 1, right, result, sb.append('('));
sb.deleteCharAt(sb.length() - 1);// <--
generate(n, left, right + 1, result, sb.append(')'));
sb.deleteCharAt(sb.length() - 1);// <--
}
or try writing something probably more readable like
public static void generate(int maxLength, int left, int right,
ArrayList<String> result, StringBuilder sb) {
if (left + right == maxLength) {
if (left == right)
result.add(sb.toString());
} else if (left >= right) {
generate(maxLength, left + 1, right, result, sb.append('('));
sb.deleteCharAt(sb.length() - 1);
generate(maxLength, left, right + 1, result, sb.append(')'));
sb.deleteCharAt(sb.length() - 1);
}
}
but while invoking you would need to set maxLength as 2*n since it is the max length StringBuilder should contain, so you would also have to change generateParenthesis(int n) to:
public static ArrayList<String> generateParenthesis(int n) {
ArrayList<String> result = new ArrayList<String>();
StringBuilder sb = new StringBuilder(2 * n);
generate(2 * n, 0, 0, result, sb);
// ^^^^^
return result;
}
Farther improvement:
If you are aiming for performance then you probably don't want to use delete or deleteCharAt because each time it creates new array and fills it with copy of values from without ones you don't want.
Instead you can use setLength method. If you will pass value which is smaller than number of currently stored characters it will set count to value passed in this method, which will effectively make characters after them irrelevant. In other words this characters will be no longer used in for instance toString() even if they will be still in StringBuilder buffer array.
Example:
StringBuilder sb = new StringBuilder("abc"); // 1
sb.setLength(2); // 2
System.out.println(sb); // 3
sb.append('d'); // 4
System.out.println(sb); // 5
In line 1 StringBuilder will allocate array for at least 3 characters (by default it uses str.length() + 16 to determine size of buffered array of characters it will store for now) and will place there characters from passed String, so it will contain
['a', 'b', 'c', '\0', '\0', ... , '\0']
^^^ - it will put next character here
Index of position where next character should be placed is stored in count field and for now it is equal to 3.
In line 2 value of count will be set to 2, but our array will not be changed so it will still look like
['a', 'b', 'c', '\0', '\0', ... , '\0']
^^^ - it will put next character here
In line 3 new String will be created and printed, but it will be filled only with characters placed before index stored in count, which means that it will contain only a and b (array will still be unchanged).
In line 4 you will add new character to buffer and it will be placed after "important" characters. Since number of important characters is stored in count field (and they are placed at beginning of array), next irrelevant character must be at position pointed by count, which means d will be placed at position with index 2 which means now array will look like
['a', 'b', 'd', '\0', '\0', ... , '\0']
^^^ - it will put next character here
and value of count will be incremented (we added only one character so count will now become 3).
In line 5 we will create and print string containing first 3 characters from array used by StringBuilder so we will see abd.
After carefully debuting this program, I found out the problem. The correct code is listed as follows:
import java.util.ArrayList;
public class generateParentheses {
public static ArrayList<String> generateParenthesis(int n) {
ArrayList<String> result = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
generate(n, 0, 0, result, sb);
return result;
}
public static void generate(int n, int left, int right, ArrayList<String> result,
StringBuilder sb) {
if (left < right) {
return;
}
if (left == n && right == n) {
result.add(sb.toString());
//sb.delete(0,sb.length());
return;
}
if (left == n) {
generate(n, left, right + 1, result, sb.append(')'));
//delete current ')'.
sb.delete(sb.length() - 1, sb.length());
return;
}
generate(n, left + 1, right, result, sb.append('('));
//delete current '(' after you finish using it for next recursion.
sb.delete(sb.length() - 1, sb.length());
generate(n, left, right + 1, result, sb.append(')'));
//same as above here.
sb.delete(sb.length() - 1, sb.length());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(generateParenthesis(4));
}
}
And the result is:
(((()))), ((()())), ((())()), ((()))(), (()(())), (()()()), (()())(), (())(()), (())()(), ()((())), ()(()()), ()(())(), ()()(()), ()()()()
The combination of a parameter, assigment and return:
..., StringBuilder sb,...
//...
sb = new StringBuilder();
return sb;
does not make sense, because the parameter is passed in by value, i.e., sb is a local variable the change of which has no effect in the calling environment.
If you want to clear the StringBuilder, there is method sb.delete( start, end ) that will truly affect the object referenced via sb.
Another much simpler way of doing this.
Here, i am trying for a recursive solution where the recursive function adds balanced parentheses in 3 ways: "()"+result and result+"()" and "("+result+")" for each item in HashSet returned by the function it calls then put them in a HashSet to remove duplicates.
import java.util.HashSet;
import java.util.Set;
public class BalancedParanthesis {
public static void main(String args[]){
int noOfBrackets = 3;
HashSet<String> hs=new HashSet(generate(noOfBrackets));
System.out.println(hs);
}
public static HashSet<String> generate(int in)
{
HashSet<String> hs= new HashSet<String>();
if(in ==1)
{
hs.add("()");
return hs;
}
else{
Set<String> ab=generate(in-1);
for(String each:ab)
{
hs.add("("+each+")");
hs.add("()"+each);
hs.add(each+"()");
}
return hs;
}
}
}
Your code is inefficient. Here is a better approach. We make an effective brute force over the 2^N possible bracket permutations with heavy pruning(we quit the recursion immediately if there is no possible valid solution for the current parameters).
Here's the code in C++:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string result;
vector<string> solutions;
int N = 10;
void generateBrackets(int pos, int balance)
{
if(balance > N-pos) return;
if(pos == N)
{
//we have a valid solution
//generate substring from 0 to N-1
//and push it to the vector
string currentSolution;
for(int i = 0; i < N; ++i)
{
currentSolution.push_back(result[i]);
}
solutions.push_back(currentSolution);
return;
}
result[pos] = '(';
generateBrackets(pos+1, balance+1);
if(balance > 0)
{
result[pos] = ')';
generateBrackets(pos+1, balance-1);
}
}
int main()
{
result.assign(N, 'a');
generateBrackets(0, 0);
cout<<"Printing solutions:\n";
for(int i = 0; i < solutions.size(); ++i)
{
cout<<solutions[i]<<endl;
}
return 0;
}
Some clarifications: pos marks the current position in the bracket solution we are generating. If we reach position N (that means the solution is valid) we have a valid solution in the string result variable and we simple push it in the vector with the valid solutions. Now for what we use balance you may ask. We make the observation that in order a bracket permutation to be valid, at any time at any given position the count of the '(' from the start must be greater than the count of the ')'s and with balance we measure their difference, so me put a ')' at a given position only if the balance > 0.

String Index Out Of Bound Exception error

I'm not really sure why I am getting this error. The code is meant to test palindromes disregarding punctuation.
So here is my code:
char junk;
String temp = "";
for (int i = 0; i < txt.length(); i++)
{
junk = txt.charAt(i);
if (Character.isLetterOrDigit(txt.charAt(jumk)))
{
temp += junk;
}
}
txt = temp;
left = 0;
right = txt.length() -1;
while (txt.charAt(left) == txt.charAt(right) && right > left)
{
left++;
right--;
}
java.lang.StringIndexOutOfBoundException : String index out of range 0
at PalindromeTester.main(PalindromeTester.java:35)
and line 35 is as following:
while (txt.charAt(left) == txt.charAt(right) && right > left)
if (Character.isLetterOrDigit(txt.charAt(yP)))
is your problem, yP is a char not a reference to a position.
What you probably meant was:
if (Character.isLetterOrDigit(yP))
Edit: My comment:
Well the value of right would be -1 and charAt would require a an integer greater than 0.. so you should check the length of txt and if it's == 0 then display a message saying an actual word is required.
You should stop execution before you get to this line:
right = txt.length() -1;
This is your fixed code:
do
{
System.out.println("Enter a word, phrase, or sentence (blank line to stop):");
txt = kb.nextLine();
}
while (!txt.equals(""));
txt = txt.toLowerCase();
char yP;
String noP = "";
for (int i = 0; i < txt.length(); i++)
{
yP = txt.charAt(i);
if (Character.isLetterOrDigit(txt.charAt(yP)))
{
noP += yP;
}
}
txt = noP;
left = 0;
right = txt.length() -1;
while (txt.charAt(left) == txt.charAt(right) && right > left)
{
left++;
right--;
}
if (left > right)
{
System.out.println("Palindrome");
cntr++;
}
else
{
System.out.println("Not a palindrome");
}
The variable yP is your character at index i, not an index (as you are using it on the line giving you the error). Change that line to:
if (Character.isLetterOrDigit(yP)) { ...
EDIT FOR THE NEW PROBLEM:
You don't need a while loop to check if the user entered nothing, since you don't want to do something repeatedly in this case (which is what loops are for). Since you only want to do something once, i.e. print out how many palindromes they have found, you can just use an if statement. The structure would look like this:
do {
get user input
if they entered the empty string "" {
print out how many palindromes they have found so far
} else { // they must have entered text, so check for palindrome
your normal palindrome checking code goes here
}
} while (your condition);
EDIT 2:
Try changing
if (left > right)
to
if (left >= right)
Since if left==right, that means they are both on the median character in an odd-length string (e.g. the 'y' in kayak) which means the string is a palindrome.

Categories

Resources