Algorithm
TakeTake a string input, such as '{([])}'. (Let's call each of the characters a 'paren'.)
0. We validate the input, check if it is a string and has at least one paren.
- We validate the input, check if it is a string and has at least one paren.
- We initialize a stack and iterate through the string input.
- If the character is a closing paren, we check to see if it is the matching, closing paren of the top stack element. Note: This checks for the order, as the latest open parens have to be closed first in a balanced string of parens.
- If the character is an open paren, we push it onto the stack
.- If step 3 fails, even once, return false.
- After the loop, if the stack is empty, return true. (If all open parens were closed in order, the stack will be empty.)
LooksIt looks like the time complexity is O(N)\$O(N)\$. HowHow can it be improved with respect to the multiple, k\$k\$, in
k. O(N)\$O(N)\$?What are some edge cases I should be testing? II tested the empty string, non-string values, and non-paren characters.