Showing posts with label pair. Show all posts
Showing posts with label pair. Show all posts

Monday, December 30, 2013

[LeetCode] Valid Parentheses


Link : Valid Parentheses

Analysis:
At first, I try to count number of each kind of parentheses in a variable, when meeting left part, increase by 1, and when meeting paired right part, decrease by 1.  If any variable below 0, return false. And if any count is not equal to 0, it is not a valid-parentheses string.

But the problem is the order of occurrence of different kind of parentheses. Counting only makes sure right part doesn't appear before left part, but cannot guarantee ([)] will return false.

I refer to http://blog.unieagle.net/2012/11/06/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Avalid-parentheses/.