java - Why does this recursive method work? -


I am writing a function to meet these needs: Given a string, if it is zero or more pairs of parentheses Like, (()) or ((())) . Tip: Check the first and the last characters, and then repeat there that is inside them. → right nestParen ("(((())" → right nestParen ("(((())") → incorrect

The correct solution is shown on the site:

  public boolean nestParen (string str) {if (str.equals ("")) is true; if (str. CharAt (0) == '(' & str.charAt (str.length () - 1) == ') back nestParen (str.substring (1, str.length () - 1)) ;;; );  

I do not understand why this works. If ( as ", in the given string Will not be killed on the second case and next (?

Definitely will not work if there is anything other than ( and ) in the input string, then this function just calls another function call this function Before doing:

  clear (string str) {string str = "(((X + y) + z);" string string for TST = ""; (int i = 0; i & lt; str.length (); i ++) {if (str.charAt (i) == '(' || Str.charAt (i) == ')') {retStr + = str .charAt (i); }} Return retStr}  

and then


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -