Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Valid Parentheses

class Solution:
    def isValid(self, s):
        if s == "":
            return True
        if len(s) < 2:
            return False

        pair_brkts = {
            "{" : "}",
            "(" : ")",
            "[" : "]"
            }
        stack = []

        for i in s:
            if i in pair_brkts:
                stack.append(i)                                #stack i(forward facing brackets) that also exists as keys in our pair_brkts dictionary)
                #print("forward facing brackets", stack)       #to see the contents of your stacked list 
            else:
                if len(stack) == 0 or pair_brkts[stack.pop()] != i:   #if stack list is empty or the value pair of last 
                                                                        #list item isnt same, return False, otherwise break out of loop
                    #print("backward facing brackets", stack)        #print to see whats left of your list after 
                                                                    #looping is over for all your i(i.e brackets)
                    return False
        if len(stack) > 0:                                          #if stack list is not empty at this point, return False, else return True
            return False
        return True

 
        

Task = Solution()
print("1. ", Task.isValid("({[()]})"))
print("2. ", Task.isValid("()[]{}"))
print("3. ", Task.isValid("(]"))
Comment

Valid parentheses

class Solution:
   def isValid (self, sequence: str):
       '''
       Function to pair if sequence contains valid parenthesis
       :param sequence: Sequence of brackets
       :return: True is sequence is valid else False
       '''
       stack = []
       opening = set('([{')
       closing = set(')]}')
       pair = {')' : '(' , ']' : '[' , '}' : '{'}
       for i in sequence :
           if i in opening :
               stack.append(i)
           if i in closing :
               if not stack :
                   return False
               elif stack.pop() != pair[i] :
                   return False
               else :
                   continue
       if not stack :
           return True
       else :
           return False

if __name__ == '__main__':
   sequence = '{[()]}'
   print(f'Is {sequence} valid ? : {Solution().isValid(sequence)}')
   sequence1 = '{[()]}{]{}}'
   print(f'Is {sequence1} valid ? : {Solution().isValid (sequence1)}')
Comment

valid parentheses

{ { } [ ] [ [ [ ] ] ] } is VALID expression
          [ [ [ ] ] ]    is VALID sub-expression
  { } [ ]                is VALID sub-expression
Comment

Valid Parentheses

class Solution {
public:
    bool isValid(string s) {
        
    }
};
Comment

Valid Parentheses

class Solution {
    public boolean isValid(String s) {
        
    }
}
Comment

Valid Parentheses



bool isValid(char * s){

}
Comment

Valid Parentheses

public class Solution {
    public bool IsValid(string s) {
        
    }
}
Comment

Valid Parentheses

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
    
};
Comment

Valid Parentheses

# @param {String} s
# @return {Boolean}
def is_valid(s)
    
end
Comment

Valid Parentheses

class Solution {
    func isValid(_ s: String) -> Bool {
        
    }
}
Comment

Valid Parentheses

class Solution {

    /**
     * @param String $s
     * @return Boolean
     */
    function isValid($s) {
        
    }
}
Comment

Valid Parentheses

function isValid(s: string): boolean {

};
Comment

PREVIOUS NEXT
Code Example
Python :: if string in lost py 
Python :: check for string in list python 
Python :: python replace n with actual new line 
Python :: create a python api 
Python :: adding debugger in django code 
Python :: how to add subtitle to plot in python 
Python :: start python server 
Python :: python all permutations of a string 
Python :: how to set numerecal index in pandas 
Python :: pyhton image resize 
Python :: decode utf8 whit python 
Python :: random seed generator minecraft 
Python :: tuple index in python 
Python :: sum of array in python 
Python :: randomly pick a value in the list 
Python :: pandas datetime to unix timestamp 
Python :: python tkinter ttk 
Python :: jupyter notebook spark 
Python :: python is space 
Python :: virtual env pyhton 
Python :: pandas apply 
Python :: list_display django foreign key 
Python :: lable on graph in matplotlib 
Python :: merge two columns name in one header pandas 
Python :: python expand nested list 
Python :: multiple figures matplotlib 
Python :: python replace one character into string 
Python :: atan2 of number python 
Python :: how to change key to value and versa in python dictionary 
Python :: how to append in dictionary in python 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =