Organizational Research By

Surprising Reserch Topic

Question:how to Check if a String is balanced?


 

I want to check if a string is balanced with recursion. I found some other posts on the forum related to this question, some answers are in programming languages that I don't understand. I can do it with a stack after reading similar questions here on Stack Overflow, how do I do it recursively?
 
private static boolean isBalanced(String s, char match)
{
    char c;
 
    if(s.isEmpty())
        return true;
 
    for(int i = 0; i < s.length(); i++)
    {
        c = s.charAt(i); 
 
        if(c == '{') 
            return isBalanced(s.substring(i+1), '}');
 
        else if(c == '[')
            return isBalanced(s.substring(i+1), ']');
 
        else if(c == '(')
            return isBalanced(s.substring(i+1), ')');
 
        // Closing matches.
        else if(c == match)
            return true;
 
    }
 
    return 
}
Please help.
 
EDIT: And no I don't want anyone to code it for me, in fact, I would appriciate knowing how to do it instead. That is why I dind't understand the answers in other languages because they are too specific to that language instead of an algorithm.
 
EDIT2: Yes balanced is {}()[] and any combination of that such as [()]
asked Sep 13, 2013 in Java Interview Questions by rajesh
edited Sep 12, 2013
0 votes
18 views



Related Hot Questions



Government Jobs Opening


...