Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a0dab51

Browse files
Add files via upload
1 parent 1e1ab9d commit a0dab51

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
2+
# An input string is valid if:
3+
# Open brackets must be closed by the same type of brackets.
4+
# Open brackets must be closed in the correct order.
5+
# Every close bracket has a corresponding open bracket of the same type.
6+
7+
# Example 1:
8+
# Input: s = "()"
9+
# Output: true
10+
11+
# Example 2:
12+
# Input: s = "()[]{}"
13+
# Output: true
14+
15+
# Example 3:
16+
# Input: s = "(]"
17+
# Output: false
18+
19+
# Example 4:
20+
# Input: s = "([])"
21+
# Output: true
22+
23+
# Constraints:
24+
# 1 <= s.length <= 104
25+
# s consists of parentheses only '()[]{}'.
26+
27+
# -------------------------------------------------------------------------------------------------------------------------------------------------
28+
29+
# STACK:
30+
31+
# We can use a stack to store characters. Iterate through the string. For an opening bracket, push it onto the stack.
32+
# If the bracket is a closing type, check for the corresponding opening bracket at the top of the stack.
33+
# If we don't find the corresponding opening bracket, immediately return false.
34+
# This works because in a valid parenthesis expression, every opening bracket must have a corresponding closing bracket.
35+
# The stack is used to process the valid string, and it should be empty after the entire process.
36+
# This ensures that there is a valid substring between each opening and closing bracket.
37+
38+
# create an empty list(stack)
39+
# create a hashmap manually; where close brackets are keys and open are values
40+
# iterate through the string
41+
# if we encounter a closed bracket, check if the stack9(stack) is not null and the top element of the stack(stack[-1]) is equal to the value of the key we just encountered(opening bracket of the closing bracket we just encountered)
42+
# if so, pop the bracket from the stack
43+
# if we encounter an open bracket, add it to the stack(append)
44+
# if stack is empty at the end, it means all brackets matched, so parenthesis are valied- return True
45+
# if stack is not empty, return False
46+
47+
48+
class Solution:
49+
def isValid(self, s: str) -> bool:
50+
51+
closeToOpen = {')':'(', ']':'[', '}':'{'}
52+
stack = []
53+
54+
for c in s:
55+
if c in closeToOpen:
56+
if stack and stack[-1] == closeToOpen[c]:
57+
stack.pop()
58+
else:
59+
return False
60+
else:
61+
stack.append(c)
62+
63+
return True if not stack else False
64+
65+
Time Complexity = O(n)
66+
Space Complexity = O(n)
67+
68+
# Companies:
69+
# Amazon- 18
70+
# Google- 17
71+
# Bloomberg- 14
72+
# Meta- 13
73+
# Microsoft- 7
74+
# LLinkedIn- 7
75+
# Apple- 6
76+
# Intuit- 4
77+
# Turing- 4
78+
# IBM- 3
79+
# Tiktok- 5
80+
# TCS- 4
81+
# Zoho- 4
82+
# Tripadvisor- 4
83+
# Walmart Labs- 3
84+
# eBay- 3
85+
# Blackrock- 3
86+
# Infosys- 2
87+
# Oracle- 2
88+
# Epic Systems- 2
89+
# Yandex- 36
90+
# Adobe- 34
91+
# Uber- 27
92+
# Yahoo- 20
93+
# JP Morgan- 18
94+
# ServiceNow- 12
95+
# Ozon- 10
96+
# Accenture- 9
97+
# EPAM Systems- 8
98+
# Goldman Sachs- 8

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /