Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

what could be the most optimum way to check balanced parenthesis?

$
0
0

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.


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>