🚀 Day 9 of daily Leetcode:
Problem Type: String
Type: Easy
Today,I solved a "VALID PARENTHESIS" problem ,I was given a string s containing just the characters '(', ')' ,'{' , '}', ']' and '['.I was supposed to determine if the input string is valid .The condition to determine whether the input string is valid or not are:
>Open brackets must be closed by the same type of brackets.
>Open brackets must be closed in the correct order.
>Every close bracket has a corresponding open bracket of the same type.
BREAKDOWN OF CODE:
1->At first a stack is initialized as an empty list. This helps us to keep track of the opening brackets encountered.
2->Now a hash map is which is created for determining the corresponding opening bracket for a given closing bracket.
3->'FOR' loop is used for iterating over each value of s
4->'IF' statement is used to check for the closed bracket in hash map
5->top_element is popped from the stack if the stack is not empty else '#' is returned as a placeholder value
6->'IF mapping[char]!=top_element' then 'False' is returned which means,it is checking if the current closing bracket(char)matches the corresponding opening bracket at the top of the stack(top_element)or not .If it doesn't match then 'False' is returned.
7->'ELSE: stack.append(char)' is used to handle open brackets which is then appended to the top of stack ,It is because all the open brackets needs to be matched with their corresponding closing bracket later in the string
8->'return not stack' returns 'True' if all brackets are matched and closed properly and 'False' otherwise.
#learninginpublic #leetcode #dailychallenge
I help engineers become extremely productive at Frontend Tech 🚀 Helping you scale giant code-bases
1mohere is the rest