-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_brac.py
More file actions
61 lines (53 loc) · 1.34 KB
/
Copy pathstring_brac.py
File metadata and controls
61 lines (53 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'''
🧩 Problem: Check if a String of Brackets is Balanced
You are given a string containing only the characters:
Copy code
( ) { } [ ]
Determine whether the string is balanced.
A string is balanced if:
Every opening bracket has a corresponding closing bracket.
Brackets are closed in the correct order.
Example 1
Input:
{[()]}
Output:
True
Example 2
Input:
{[(])}
Output:
False
Example 3
Input:
((()
Output:
False
💡 Constraint
Length of string can be up to 10⁵.
'''
# Solution
def brackets_check(string):
brackets_dict = {")":"(","}":"{","]":"["}
stack = []
if len(string) > 10**5:
return f"{False}\nBecause your's string wants to accquire memory more than the system's total memory "
for char in string:
if char in brackets_dict.values():
stack.append(char)
elif char in brackets_dict.keys():
if not stack:
return False
if stack[-1] != brackets_dict[char]:
return False
stack.pop()
else:
if len(stack) > 0:
return f"{False}\nYou were in right track initialy, but for some reasons you went astray \nand started entering any random values"
else:
return f"{False}\nBecause you are entering input other than the original input"
return len(stack) == 0
def main(test):
print(f"result: {brackets_check(test)}")
if __name__ == "__main__":
testcase = input("enter the input of your choice: ")
main(testcase)