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
}
Balanced is {}()[] and any combination of that such as [()]
I don't want anyone to code it for me, in fact, I would appreciate knowing how to do it instead. That is why I didn't understand the answers in other languages because they are too specific to that language instead of an algorithm.