1
00:00:00,000 --> 00:00:03,800
A stack processes the most recently stored
value first.

2
00:00:04,080 --> 00:00:08,960
This last-in, first-out rule matches
problems where the latest unfinished item

3
00:00:09,040 --> 00:00:11,800
must be resolved before an earlier item.

4
00:00:12,080 --> 00:00:14,560
Our example checks nested brackets.

5
00:00:14,800 --> 00:00:19,520
A closing bracket must match the most
recent unmatched opening bracket, which is

6
00:00:19,640 --> 00:00:21,840
exactly the value at the top of a stack.

7
00:00:22,320 --> 00:00:26,000
Python lists provide the append and pop
operations we need.

8
00:00:26,300 --> 00:00:29,180
Push adds a value at the end of a list.

9
00:00:29,220 --> 00:00:32,500
Peek reads the last value without removing
it.

10
00:00:32,540 --> 00:00:35,793
Pop removes that last value and returns
it.

11
00:00:35,847 --> 00:00:38,380
The end of the list is the stack's top.

12
00:00:38,460 --> 00:00:42,620
Check that the stack is nonempty before
peeking or popping when an empty stack is

13
00:00:43,100 --> 00:00:43,633
possible.

14
00:00:43,687 --> 00:00:47,740
End operations avoid shifting every later
element, which would happen if we

15
00:00:47,820 --> 00:00:50,220
repeatedly removed position zero.

16
00:00:50,467 --> 00:00:52,947
Push four into an empty stack.

17
00:00:53,067 --> 00:00:55,587
Push seven, which becomes the new top.

18
00:00:55,707 --> 00:00:58,787
Push nine, which becomes the top above
seven.

19
00:00:58,907 --> 00:01:02,040
Pop returns nine and leaves four and
seven.

20
00:01:02,094 --> 00:01:05,467
A peek now returns seven without changing
the stack.

21
00:01:05,507 --> 00:01:09,640
These operations concern arrival order,
not numeric order.

22
00:01:09,694 --> 00:01:14,227
A stack does not choose the largest value,
and it does not search for a matching item

23
00:01:14,387 --> 00:01:15,427
below the top.

24
00:01:15,667 --> 00:01:18,067
Read the parenthesis and push it.

25
00:01:18,187 --> 00:01:21,707
Read the square bracket and push it above
the parenthesis.

26
00:01:21,747 --> 00:01:25,387
Read the brace and push it above both
earlier openings.

27
00:01:25,427 --> 00:01:29,267
The stack contains exactly the openings
that have not yet matched a closing

28
00:01:29,347 --> 00:01:31,467
bracket, in their nesting order.

29
00:01:31,507 --> 00:01:36,027
The brace is the only opening that the
next closing bracket is allowed to match.

30
00:01:36,067 --> 00:01:39,187
Matching an older opening would cross the
nesting order.

31
00:01:39,433 --> 00:01:42,513
Close the brace and pop its matching
opening.

32
00:01:42,553 --> 00:01:46,913
Close the square bracket and pop the
square opening, which is now on top.

33
00:01:46,953 --> 00:01:50,126
Close the parenthesis and pop the final
opening.

34
00:01:50,180 --> 00:01:54,593
The stack is empty and the input is
exhausted, so all brackets match.

35
00:01:54,633 --> 00:01:58,193
A mismatch at any step would return false
immediately.

36
00:01:58,233 --> 00:02:02,233
A closing bracket with an empty stack also
fails because there is no opening

37
00:02:02,313 --> 00:02:03,913
available to match it.

38
00:02:04,133 --> 00:02:07,653
Validate the input characters before the
main scan.

39
00:02:07,693 --> 00:02:11,946
The dictionary maps each closing bracket
to the opening it requires.

40
00:02:12,000 --> 00:02:14,733
An opening character is appended to the
stack.

41
00:02:14,773 --> 00:02:19,573
A closing character fails if the stack is
empty or if its popped value differs

42
00:02:19,653 --> 00:02:21,333
from the required opening.

43
00:02:21,493 --> 00:02:24,413
Return true only when the final stack is
empty.

44
00:02:24,453 --> 00:02:28,613
An input with unfinished openings
therefore fails even if no mismatch

45
00:02:28,693 --> 00:02:30,293
occurred during the scan.

46
00:02:30,533 --> 00:02:34,853
The scan visits each character once after
a separate linear validation

47
00:02:34,933 --> 00:02:35,973
pass.

48
00:02:36,093 --> 00:02:39,933
Each opening is pushed once and popped at
most once.

49
00:02:39,973 --> 00:02:44,893
End operations take constant amortized
time, so the total time is linear.

50
00:02:44,933 --> 00:02:49,733
The stack can contain every character when
the input consists only of openings, which

51
00:02:49,813 --> 00:02:52,586
makes worst-case auxiliary space linear.

52
00:02:52,640 --> 00:02:57,173
A short input may return early, but that
does not change the worst-case bound.

53
00:02:57,400 --> 00:03:01,720
Equal opening and closing counts do not
prove correct nesting.

54
00:03:01,760 --> 00:03:06,093
A crossed sequence can contain all the
right characters in the wrong order.

55
00:03:06,147 --> 00:03:09,920
An unfinished opening leaves the stack
nonempty at the end.

56
00:03:09,960 --> 00:03:13,120
A leading closing bracket finds an empty
stack.

57
00:03:13,160 --> 00:03:17,680
Test those cases and the empty string,
which is valid under this contract.

58
00:03:17,720 --> 00:03:21,720
During an interview, explain the
unmatched-opening invariant before

59
00:03:21,960 --> 00:03:23,400
implementing the condition.
