Find the error

//https://leetcode.com/problems/valid-parentheses/submissions/
bool isValid(char * s){
int i=0,flag=0;
while(s[i]<=strlen(s))
{
if(((int)s[i]!=s[i+1]-1)||((int)s[i]!=s[i+1]-2))
flag++;
i=i+2;
}
if(flag>0)
return false;
return true;
}

What is the problem name?? add link to it and also add your submission link and describe your approach Only than we can help you :slightly_smiling_face:

.

edited

What is your approach to solve this problem ??Mention it and add that problem link as link so we can directly navigate to it.

i have shared link in the first comment
and i am comparing the ascii value of braces, if they dont match im incrementing flag and i am taking two characters at a time and stepping i by 2

Your code fails on sample test. Because approach is wrong. if expression is “[[{()}]]” then if unmatched bracket occurs you should ensure that last unmatched bracket should be matched first whatever type of bracket it is. If you think about this problem structure you will be able to solve it.
You are not thinking about order of brackets just think about this case.
([)]

Just think about Stack .Problem structure and stack concept is same.

1 Like

If you want more help I can provide you some demo how the right algorithm will work. Or I can provide solution with approach.

yes please provide solution with approach

def isValid(self, s: str) -> bool:
        
        length=len(s) #Length of Expression
        
        matching_brackets={"(":")","{":"}","[":"]"}
        
        # This will help us to determine whether opening bracket is matched with          corresponding closing bracket. It is necessary because there are 3 types of Brackets.
        
        
        stack=[]
        
        # A stack which will store opening brackets. It will also be helpful to pop last unmatched bracket. 
        
        
        opening_brackets=["(","{","["]
        
        # List of Unmatched Opening Brackets.
        
        
        for i in range(length): 
            
            # Traverse the expression of Brackets
            
            if s[i] in opening_brackets:  
                
                # if current Bracket is opening bracket just push into stack.
                
                stack.append(s[i])
                
            else:
                
                
                if len(stack) !=0 and matching_brackets[stack[-1]] == s[i]:
                    
                    # if stack is not empty and closing bracket comes in expressioon s then check whether it is corresponding closing bracket of last unmatched opening brakcet.
                    
                    #stack[-1] will give last unmatched opening bracket.Same as stack[len(stack)-1]
                    
                    # matching_brackets[stack[-1]] will give corresponding matched  closig bracket of unmatched opening bracket.
                    
                    # print(stack[-1],s[i])

                    # uncomment above line to verify solution approach.
                    
                    stack.pop(-1)
                    
                else:
                    
                    #if stack is empty and closing brackets comes in s then return False
                    
                    return False
                
        if len(stack)==0:
            
            #This condition to check whether all the unmatched Brackets are matched or not. Stack will be empty when everything is matched.
            
            return True
        
        else:
            return False

**I have commented all the statements to describe how it is working. You should first try to solve same approach using pen and paper. then uncomment that line to see it is working same. **

Try to implement same in Your language.

1 Like