diff --git a/Python/20_Valid_parentheses.py b/Python/20_Valid_parentheses.py new file mode 100644 index 00000000..6114ae70 --- /dev/null +++ b/Python/20_Valid_parentheses.py @@ -0,0 +1,21 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + for i in range(len(s)): + if s[i] == "(" or s[i] == "[" or s[i] == "{": #check if an open bracket, push to stack + stack.append(s[i]) + if s[i] == ")" or s[i] == "]" or s[i] == "}": #check if closing bracket, pop from stack and compare + if len(stack) == 0: + return False + prev = stack.pop() + if prev == "(" and s[i] != ")": + return False + if prev == "[" and s[i] != "]": + return False + if prev == "{" and s[i] != "}": + return False + + if len(stack) == 0: + return True + else: + return False \ No newline at end of file diff --git a/Python/26_Remove_Duplicates_from_Sorted_Array.py b/Python/26_Remove_Duplicates_from_Sorted_Array.py new file mode 100644 index 00000000..a62f3a97 --- /dev/null +++ b/Python/26_Remove_Duplicates_from_Sorted_Array.py @@ -0,0 +1,8 @@ +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + i = 1 + while i List[int]: + for i in range(len(nums)): + for j in range(i+1, len(nums)): + if nums[i] + nums[j] == target: + ans = [i, j] + return ans \ No newline at end of file