Valid Parentheses

Valid Parentheses
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.

(Java Code on Github at the bottom of the post. )

Q: What is the unknown?
A: determine if the input string is valid.
Q: What does it mean by valid?
A: Parentheses must be in the correct order:
1. Each (, [, { must correspond to one ), ], }.
2. ), ], } cannot exist without a preceding (, [, {.
3. The parentheses must not intertwine like ([)], however {([])} is allowed.
Q: So these are also the constraints then. What about the data, input and output?
A: The input data is a string and the output is a boolean.
Q: Special cases we should pay attention to?
A: Null string, empty string, string with odd length, string with characters other than (), [] and {}. The algorithm should return false in these cases.

Q: OK. Have you seen it before? Or something similar?
A: Yes I’ve seen something similar. In that problem, using a Stack is helpful.
Q: And why is that?
A: Well, the validation actually happens when we meet one of the three characters ), ] and }. At that point, we need to check if there is a preceding (, [, or {. A stack helps us to keep track of what we have seen previously and easily get back to it.
Q: Fair enough. So how would you use the stack here? How does it operate?
A: Hmm, whenever we meet a (, [ or {, we push it into the stack. In other cases, we first check if the stack is empty. If yes, then return false. If no, then check the top character of the stack matches in pair with the current character. If yes, we pop the stack. Otherwise, return false.
Q: OK, then when do we return true?
A: When we finish scanning the string, if the stack is empty then we return true. We must check this otherwise it will fail on test case like “((“.
Q: Good. What is the complexity of this algorithm?
A: The time and space complexities are both O(n) where n is the length of the string since we only scan the string once but we need the stack to save the characters.

Code on github:

Update:
Q: can we do better?
A: well we’ve already achieved O(n) time complexity. I’m not sure if we can make it even faster. After all, we do have to check all characters.
Q: Then what about space? Is the stack really necessary?
A: Hmm, you are saying that we should use something else to keep track of previous characters?
Q: Yep, the characters are in the string, right? I can access them easily using index.
A: OK, I see where this is going. So instead of using a stack, we should consider using indices to keep track of previous left characters?
Q: Yeah.
A: Hmm, in that case, we need the index of the latest left character and its previous character and this shall apply to all seen left characters and we will need extra space to either create a wrapper class to hold the index and this will cost O(n) space too. So it’s not better.
Q: OK, fair enough. But it’s always good to think about possible improvement.
A: Of course.

Advertisements
Valid Parentheses

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s