Valid parentheses
Given A string containing just the characters,, ‘(‘, ‘)‘
‘{‘
, and ‘}‘
‘[‘
‘]‘
, determine if the input string is valid .
Example
The brackets must close in the correct order, and is all valid but and is not "()"
"()[]{}"
"(]"
"([)]"
.
////
The substring () method method in Java string is as follows: public string substring (int beginindex, int endIndex) The first int is the starting index, corresponding to the start position in the string number, The second is the cutoff index position, corresponding to the end position in string 1, the length of the string obtained is: Endindex-beginindex;2, starting from Beginindex, to the end of the EndIndex, starting from 0 number, The word such as: "Hamburger" is not included in the Endindex position. SUBSTRING (4, 8) returns "Urge" "smiles". Substring (1, 5) returns "Mile"////////
Public classSolution {/** * @params A String *@returnwhether the string is a valid parentheses*/ Public Booleanisvalidparentheses (String s) {if(s==NULL)return false; if(S.length ()%2!=0)return false; //Write Your code here Char[] c=S.tochararray (); for(intI=1;i<s.length (); i++) { Booleanr1= c[i-1]== ' (' && c[i]== ') '; Booleanr2= c[i-1]== ' {' && c[i]== '} '; Booleanr3= c[i-1]== ' [' && c[i]== ']; if(r1| | r2| |R3) {String S1=s.substring (0,i-1) +s.substring (i+1); if(S1.length () ==0)return true; Else returnisvalidparentheses (S1); } } return false; }}
[Lintcode Easy] Valid parentheses