I have one string which can only have '(' and ')' ex: ((())) or ()()() now I want to check whether this parenthesis is balanced or not.
static boolean isValidSequence(char []s){
char[] temp=new char[s.length]; //stack
int k=-1;
for (int i=0;i<s.length ; i++) {
if (s[i]=='(') {
temp[++k]='(';
}
else k--;
}
if(k==-1)return true;
return false;
}
This is my solution for the same, please tell me this solutions is correct or not. Could there be any more optimal way to achieve this.
Also show me the recursive implementation for this problem.