PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 19, 2022

[FIXED] How to best store data when working with recursion, and reset the data if method is re-called

 July 19, 2022     integer, java, recursion     No comments   

Issue

I'm tasked with searching recursively and iteratively for a string in a given input. More specifically I need to ensure that all parentheses brackets have an opposite one to match it. My code returns true if paratheses are all matched and false if they aren't. My problem is with the recursion as I'm struggling to track the parentheses. My code beneath works perfectly when called once, but if I call it multiple times the integers cross-track and it causes out of bounds exceptions. I want to know the best way that I can store the values when calling the function, as well as how I can reset the integers when the function gets called again.

In other words, how can I reset these numbers after calling the code so that it works when called again, and what's the best way to track a number within recursion?

public static int tempSpot = 0; // this integer stores the position of the string im at
                                // since I'm not allowed to use a for loop to serve the same purpose
public static int openCount1 = 0; // track the count of "("
public static int closeCount1 = 0; // track the count of ")"

    public static boolean balanceParenthesesRecursive(String str){
        if (str.charAt(tempSpot) == '(') {
            openCount1++;
        }
        if (str.charAt(tempSpot) == ')') {
            closeCount1++;
        }
        tempSpot++;
        if (tempSpot == str.length()) {  // prevents recursion if next call will be out of bounds
            return openCount1 == closeCount1;
        } else {
            balanceParenthesesRecursive(str);
        }
        return openCount1 == closeCount1;
    }

Solution

I believe, you do not need to "store" any data. You may pass the data as parameters to the next recursion loop. Something like that:

public static boolean balanceParenthesesRecursive(
    String str, int tempSpot, int openCount, int closeCount
) {
    if (str.charAt(tempSpot) == '(') {
        openCount++;
    }
    if (str.charAt(tempSpot) == ')') {
        closeCount++;
    }
    tempSpot++;
    if (tempSpot == str.length()) {
        return openCount == closeCount;
    } else {
        return balanceParenthesesRecursive(str, tempSpot, openCount, closeCount);
    }
}

public static void main(String[] args) {
    System.out.println("Positive result = " + balanceParenthesesRecursive("this is (positive) test string.", 0, 0, 0));
    System.out.println("Negative result = " + balanceParenthesesRecursive("this is (negative)) test string.", 0, 0, 0));
}

Update: You said that you have a requirement that the method should accept only one parameter (the string). There is an alternative, but it is really ugly one. To clear the static variables when the work is done:

public static int tempSpot = 0; // this integer stores the position of the string im at
// since I'm not allowed to use a for loop to serve the same purpose
public static int openCount1 = 0;
public static int closeCount1 = 0; // track the count of "(" or ")"

public static boolean balanceParenthesesRecursive(String str){
    if (str.charAt(tempSpot) == '('){
        openCount1++;
    }
    if (str.charAt(tempSpot) == ')'){
        closeCount1++;
    }
    tempSpot++;
    if (tempSpot == str.length()){  // prevents recursion if next call will be out of bounds
        boolean result = openCount1 == closeCount1;
        tempSpot = 0;
        openCount1 = 0;
        closeCount1 = 0;
        return result;
    } else {
        return balanceParenthesesRecursive(str);
    }
}

public static void main(String[] args) {
    System.out.println("Positive result = " + balanceParenthesesRecursive("this is (positive) test string."));
    System.out.println("Negative result = " + balanceParenthesesRecursive("this is (negative)) test string."));
}

Alternative #2. Maybe it's ok to "wrap" recursive method into non-recursive:

public static boolean balanceParenthesesRecursive(
    String str, int tempSpot, int openCount, int closeCount
) {
    if (str.charAt(tempSpot) == '(') {
        openCount++;
    }
    if (str.charAt(tempSpot) == ')') {
        closeCount++;
    }
    tempSpot++;
    if (tempSpot == str.length()) {
        return openCount == closeCount;
    } else {
        return balanceParenthesesRecursive(str, tempSpot, openCount, closeCount);
    }
}

public static boolean balanceParentheses(String str) {
    return balanceParenthesesRecursive(str, 0, 0, 0);
}

public static void main(String[] args) {
    System.out.println("Positive result = " + balanceParentheses("this is (positive) test string."));
    System.out.println("Negative result = " + balanceParentheses("this is (negative)) test string."));
}


Answered By - Andrej Istomin
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing