if statement - Determining nested parentheses by recursion(JAVA) -


I am trying to write a method that determines the nested pair of parentheses in the string. Example: "(true)" ((4)) "false" () () "false" ((()) "is false

  public stable bullion nested brackets ( Strings) {if (s.length () & lt; 4) {return false;} else if (s.charAt (0) == '(' & amp; s.charAt (s.length () - 1 ) == ')') {if (scharar (1) == '(' & amp; s.charAt (s.length () - 2) == ')' & amp; amp; amp; amp; amp; = 4) {Return true;} And if (S. Charts at (2) == '(' and S. S.) (S. Long (3) == ')' & amp; amp; .length () == 6) {back true;} other {return false;}} else {return false;  

Since your question has been "recurring", offering a recursive solution Is performed. Public stable boolean nested brackets (strings) {if (s.length () & lt; 2) {return false;} If (SRTT 0) ! = '(' ') (Return false;) If (S.A.T.T.T. (S.Langha () - 1)! =') ') {Return lie;} if (s.length () = = 2) {return true;} Return nested brackets (s.sstring (1, s.length () - 1));}

The implementation is straightforward. The hostel with each rec, make sure the entire expression is inside the bracket, then revisit the contents within the brackets, the terminal condition is reached for the single pair of brackets: "()".

Note: You do not specify the expected result for the empty string case (""). Return to the empty string false The above solutions below.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -