Both these algorithms determine if all the parenthesis in a string are balanced and properly nested.
The first algorithm uses "a constant amount of memory, regardless of the length of the string."
The second algorithm "may use O(n) memory but must use a stack".
I understand how the algorithms work but I don't understand the descriptions of time complexity.
My understanding is that they are both O(n) because they both use a single for loop and the length of time it will take to execute them depends on the size of the string.
Also on algorithm 2 I believe the last bit should be if t!=0 instead of ==?
Any help understanding these descriptions of time complexity would be greatly appreciated. Thanks.
s(i)
,t.push()
,t.pop()
,t.length
? – greybeard Apr 09 '22 at 06:50