|
|
|
|
|
by jibal
213 days ago
|
|
If you've encountered 1 million unclosed parentheses, any or all of them could be unbalanced, so to report which ones are, you need 1 million pieces of information. The obvious way to organize them is as stack. Of course there are worse ways to do it. Rewinding the position pointer means that you've kept the entire input as a stack of characters, and now you have to keep track of all the closers on a stack in order to balance them with their openers. You NEED a stack. (And no, I didn't presume anything ... I addressed rewinding above.) |
|
(and yes, you did presume something; if you have rewindable file handle, you do not need to keep the characters; you can instead-re-read them)