CodeNewbie Community ๐ŸŒฑ

Cover image for Python ๐Ÿ challenge_25โš”๏ธ
Mahmoud_Essam
Mahmoud_Essam

Posted on

Python ๐Ÿ challenge_25โš”๏ธ

Valid Braces

  • Write a function that takes a string of braces, and determines if the order of the braces is valid.
  • It should return true if the string is valid, and false if it's invalid.
  • All input strings will be nonempty, and will only consist of parentheses, brackets and curly braces: ()[]{}.
  • What is considered Valid?
  • A string of braces is considered valid if all braces are matched with the correct brace.

Examples:

    "(){}[]"   =>  True
    "([{}])"   =>  True
    "(}"       =>  False
    "[(])"     =>  False
    "[({})](]" =>  False
Enter fullscreen mode Exit fullscreen mode
Task URL: Link

My Solution:

def validBraces(string):

    brackets = ['()', '{}', '[]']
    while any(x in string for x in brackets):
        for br in brackets:
            string = string.replace(br, '')
    return not string
Enter fullscreen mode Exit fullscreen mode

Another Solution:

def validBraces(string):

    brackets = ['()', '{}', '[]']
    while any(x in string for x in brackets):
        for br in brackets:
            string = string.replace(br, '')
    return not string
Enter fullscreen mode Exit fullscreen mode

Code Snapshot:

Image description

Image description

Learn Python

Python top free courses from Coursera๐Ÿ๐Ÿ’ฏ๐Ÿš€

๐ŸŽฅ

Connect with Me ๐Ÿ˜Š

๐Ÿ”— Links

linkedin

twitter

Top comments (0)