SlideShare a Scribd company logo
Stacks
Rodrigo Villarreal && Paulo Almeida
Auckland Data Structures and
Algorithms
Agenda
● Introduction to Stacks
○ Design
○ Real-world applications
● Practice
○ Examples
○ Let’s get our hands dirty!
● What’s next?
What is a Stack ?
A stack is an abstract data type that serves as a collection of
elements, with two principal operations:
1 1 2 3 5 6bottom top
What is a Stack ?
A stack is an abstract data type that serves as a collection of
elements, with two principal operations:
1 1 2 3 5
6
pop()bottom
What is a Stack ?
A stack is an abstract data type that serves as a collection of
elements, with two principal operations:
1 1 2 3 5
8
push(x)
bottom
What is a Stack ?
A stack is an abstract data type that serves as a collection of
elements, with two principal operations:
1 1 2 3 5 8
Both operations change the top of the stack.
bottom top
What is a Stack ?
This behaviour is sometimes referred as LIFO (Last in, First
out).
What is a Stack ?
This behaviour is sometimes referred to as LIFO (Last in, First
out).
1
push(x)
bottom top
What is a Stack ?
This behaviour is sometimes referred to as LIFO (Last in, First
out).
1
push(x)
1
bottom
What is a Stack ?
This behaviour is sometimes referred to as LIFO (Last in, First
out).
1
push(x)
2
1bottom
What is a Stack ?
This behaviour is sometimes referred to as LIFO (Last in, First
out).
1
push(x)
3
1 2bottom
What is a Stack ?
This behaviour is sometimes referred to as LIFO (Last in, First
out).
1 31 2bottom top
What is a Stack ?
This behaviour is sometimes referred to as LIFO (Last in, First
out).
1 pop()
3
1 2bottom
What is a Stack ?
This behaviour is sometimes referred to as LIFO (Last in, First
out).
1 pop()
2
1bottom
What is a Stack ?
This behaviour is sometimes referred to as LIFO (Last in, First
out).
1 pop()
1
bottom
What is a Stack ?
This behaviour is sometimes referred to as LIFO (Last in, First
out).
1
pop()
bottom top
What is a Stack ?
Like in a stack of books. One can
only add or remove from the
top of the stack.
….
(Or push/pop respectively)
Big O
Time Complexity (Avg/Worst)
Search: O(n)
Access: O(n)
Insertion: O(1)
Deletion: O(1)
Space Complexity (Worst): O(n)
Where would I use Stacks in the real world ?
I’m glad you asked :)
Real life examples
Parsing mathematical expressions
String expr = ( 9 + ( 4 / 2 ) ) * 2
Real life examples
Parsing mathematical expressions
String expr =
(
push(x)
bottom top
( 9 + ( 4 / 2 ) ) * 2
String expr =
(bottom top
( 9 + ( 4 / 2 ) ) * 2
Real life examples
Parsing mathematical expressions
String expr =
(bottom
push(x)
9
( 9 + ( 4 / 2 ) ) * 2
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9
( 9 + ( 4 / 2 ) ) * 2
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9
push(x)
+
( 9 + ( 4 / 2 ) ) * 2
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 +
( 9 + ( 4 / 2 ) ) * 2
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 +
push(x)
(
( 9 + ( 4 / 2 ) ) * 2
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 + (
( 9 + ( 4 / 2 ) ) * 2
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 + (
push(x)
4
( 9 + ( 4 / 2 ) ) * 2
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 + ( 4
( 9 + ( 4 / 2 ) ) * 2
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 + ( 4
( 9 + ( 4 / 2 ) ) * 2
push(x)
/
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 + ( 4
( 9 + ( 4 / 2 ) ) * 2
/
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 + ( 4
( 9 + ( 4 / 2 ) ) * 2
/
push(x)
2
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 + ( 4
( 9 + ( 4 / 2 ) ) * 2
/ 2
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 + ( 4
( 9 + ( 4 / 2 ) ) * 2
/ 2
pop()pop()pop()pop()
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 +
( 9 + ( 4 / 2 ) ) * 2
push(x)
2
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 +
( 9 + ( 4 / 2 ) ) * 2
2
Real life examples
Parsing mathematical expressions
String expr =
(bottom 9 +
( 9 + ( 4 / 2 ) ) * 2
2
pop()pop()pop()pop()
Real life examples
Parsing mathematical expressions
String expr =
bottom
( 9 + ( 4 / 2 ) ) * 2
11
push(x)
top
Real life examples
Parsing mathematical expressions
String expr =
bottom
( 9 + ( 4 / 2 ) ) * 2
11 top
Real life examples
Parsing mathematical expressions
String expr =
bottom
( 9 + ( 4 / 2 ) ) * 2
11
push(x)
*
Real life examples
Parsing mathematical expressions
String expr =
bottom
( 9 + ( 4 / 2 ) ) * 2
11 *
Real life examples
Parsing mathematical expressions
String expr =
bottom
( 9 + ( 4 / 2 ) ) * 2
11 *
push(x)
2
Real life examples
Parsing mathematical expressions
String expr =
bottom
( 9 + ( 4 / 2 ) ) * 2
11 * 2
Real life examples
Parsing mathematical expressions
String expr =
bottom
( 9 + ( 4 / 2 ) ) * 2
11 * 2
pop()pop()pop()
Real life examples
Parsing mathematical expressions
String expr = ( 9 + ( 4 / 2 ) ) * 2
bottom 22
push(x)
top
Real life examples
Parsing mathematical expressions
Using Stacks in Java
Declare stack:
Stack<Type> stack = new Stack<>();
Push element:
stack.push(element);
Pop element:
stack.pop();
Valid Parentheses (L #20)
Given a string containing just the characters
determine if the input string is valid.
An input string is valid if:
● Open brackets must be closed by the same type of brackets.
● Open brackets must be closed in the correct order.
( ) { } [ ]
Valid Parentheses (L #20)
Test cases
Example Output
“()” true
“()[]{}” true
“(]” false
“([)]” false
“{[]}” true
DIVISORIA
DIVISORIA
Valid Parentheses (L #20)
Time Complexity = O(n)
Space Complexity = O(n)
Still not convinced… where else would I use
stacks?
Can you relate to this example?
Real life examples
Back / Forward Browser Button
Back Stack Forward Stack
Real life examples
Back / Forward Browser Button
Back Stack Forward Stack
www.google.comcurrent
Real life examples
Back / Forward Browser Button
Back Stack Forward Stack
www.google.com
www.facebook.comcurrent
Real life examples
Back / Forward Browser Button
Back Stack Forward Stack
www.google.com
www.facebook.com
current www.twitter.com
Real life examples
Back / Forward Browser Button
Back Stack Forward Stack
www.google.com
www.facebook.com
current
www.twitter.com
www.meetup.com
Real life examples
Back / Forward Browser Button
Back Stack Forward Stack
www.google.com
www.facebook.com
current
www.twitter.com
www.meetup.com
www.linkedin.com
Real life examples
Back / Forward Browser Button
Back Stack Forward Stack
www.google.com
www.facebook.com
current
www.twitter.com
www.meetup.com
www.linkedin.com
Real life examples
Back / Forward Browser Button
Back Stack Forward Stack
www.google.com
www.facebook.com
current www.twitter.com
www.meetup.com
www.linkedin.com
Real life examples
Back / Forward Browser Button
Real life examples
Back / Forward Browser Button
What happens if I write a new URL while I am in Twitter?
Back Stack Forward Stack
www.google.com
www.facebook.com
current www.twitter.com
www.meetup.com
www.linkedin.com
Real life examples
Back / Forward Browser Button
Forward Stack gets cleared
Back Stack Forward Stack
www.google.com
www.facebook.com
current
www.twitter.com
www.gmail.com
Remove All Adjacent Duplicates In String (L
#1047)
Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal
letters, and removing them.
We repeatedly make duplicate removals on S until we no longer can.
Return the final string after all such duplicate removals have been made. It is guaranteed the answer is
unique.
Input: "abbaca"
Output: "ca"
Explanation: For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal,
and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is
possible, so the final string is "ca".
Remove All Adjacent Duplicates In String (L
#1047)
Test cases
Input: "abbaca"
Output: "ca"
Input: "abcdeedcba"
Output: ""
Input: "abcdefg"
Output: "abcdefg"
Remove All Adjacent Duplicates In String (L
#1047)
Approach #1
Iterate the string and remove the adjacents. If we find a pair, we set a flag to true indicating we
need to iterate again (since we have created a new string)
Input: "abcdeedcba"
Iteration #1: "abcddcba" Iteration #5: "abba"
Iteration #2: "abccba" Iteration #6: "aa"
Iteration #3: "abccba" Iteration #7 : ""
Iteration #4: "abccba"
Remove All Adjacent Duplicates In String (L
#1047)
Approach #1
Time Complexity = O(n^2)
Space Complexity = O(n)
Remove All Adjacent Duplicates In String (L
#1047)
Approach #2
We’ll use a stack this time as our support tool to solve this problem. We’ll iterate the string and
we’ll check if the stack has elements and if top of the stack (the previous char) is equal to the
element we’re currently checking.
If the previous statement is false, add the char to the stack
If the previous statement is true, pop the previous char and don’t add the current to the stack
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
a
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
a
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
a b
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
a b
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
a
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
a
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
c
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
c
Remove All Adjacent Duplicates In String (L
#1047)
a b b a c a
Input
Stack
c a
Remove All Adjacent Duplicates In String (L
#1047)
Final Result
c a
Remove All Adjacent Duplicates In String (L
#1047)
Approach #1
Time Complexity = O(n^2)
Space Complexity = O(n)
Approach #2
Time Complexity = O(n)
Space Complexity = O(n)
Remove All Adjacent Duplicates In String (L
#1047)
Approach #1
Time Complexity = O(n^2)
Space Complexity = O(n)
Approach #2 ← Winner!
Time Complexity = O(n)
Space Complexity = O(n)
Auckland Programming Algorithms and Performance Meetup about Stacks & LeetCode Problems
Leetcode Problems
Easy
Remove All Adjacent Duplicates In String (Leetcode #1047)
Valid Parentheses (Leetcode #20)
Min Stack (Leetcode #155)
Decode String (Leetcode #394)
Medium
Design Browser History (Leetcode #1472)
Remove K Digits (Leetcode #402)
Online Stock Span (Leetcode #901)
Hard
Basic Calculator (Leetcode #224)
What next?
The next session will be about Trees!

More Related Content

PDF
Go Java, Go!
PDF
Declarative Thinking, Declarative Practice
PDF
Matlab and Python: Basic Operations
PDF
Don't Go, Java!
PDF
Learn 90% of Python in 90 Minutes
PDF
Constructs and techniques and their implementation in different languages
PDF
Python and sysadmin I
PDF
AmI 2016 - Python basics
Go Java, Go!
Declarative Thinking, Declarative Practice
Matlab and Python: Basic Operations
Don't Go, Java!
Learn 90% of Python in 90 Minutes
Constructs and techniques and their implementation in different languages
Python and sysadmin I
AmI 2016 - Python basics

What's hot (19)

PDF
Fun never stops. introduction to haskell programming language
PPTX
Python 101++: Let's Get Down to Business!
PPT
Introduction to Python - Part Two
PPTX
Java 8 Puzzlers [as presented at OSCON 2016]
PDF
AmI 2015 - Python basics
PDF
Lab report for Prolog program in artificial intelligence.
PDF
Python
PPT
Euro python2011 High Performance Python
PPTX
Python Traning presentation
PDF
LL Parsing
ODP
Clojure made simple - Lightning talk
PDF
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
PDF
Spring scala - Sneaking Scala into your corporation
PDF
Python Performance 101
PPTX
Python language data types
PPT
Profiling and optimization
PDF
Don't do this
PPT
Introduction to Python - Part Three
PDF
Functional Programming & Event Sourcing - a pair made in heaven
Fun never stops. introduction to haskell programming language
Python 101++: Let's Get Down to Business!
Introduction to Python - Part Two
Java 8 Puzzlers [as presented at OSCON 2016]
AmI 2015 - Python basics
Lab report for Prolog program in artificial intelligence.
Python
Euro python2011 High Performance Python
Python Traning presentation
LL Parsing
Clojure made simple - Lightning talk
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
Spring scala - Sneaking Scala into your corporation
Python Performance 101
Python language data types
Profiling and optimization
Don't do this
Introduction to Python - Part Three
Functional Programming & Event Sourcing - a pair made in heaven
Ad

Similar to Auckland Programming Algorithms and Performance Meetup about Stacks & LeetCode Problems (20)

PPT
Stack application
PPT
PPT
week 7,8,10,11 alll files included from .ppt
PDF
PPTX
Stacks and queues using aaray line .pptx
PPT
Stacks.ppt
PPT
Stacks.ppt
PPTX
dsppt-141121224848-conversion01 (1).pptx
PPTX
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
PPTX
Chapter 5-stack.pptx
PPTX
PPTX
STACKS IN DATASTRUCTURE
PPTX
DS-UNIT 3 FINAL.pptx
PPT
Stack in Data Structure
PDF
Data Structures And Algorithms(stacks queues)
PPTX
stack and types of stack with algorithms and stack
PPTX
Stack and its Applications : Data Structures ADT
PPTX
Unit I.pptx
PPTX
Stack - Data Structure - Notes
PPTX
Project on stack Data structure
Stack application
week 7,8,10,11 alll files included from .ppt
Stacks and queues using aaray line .pptx
Stacks.ppt
Stacks.ppt
dsppt-141121224848-conversion01 (1).pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
Chapter 5-stack.pptx
STACKS IN DATASTRUCTURE
DS-UNIT 3 FINAL.pptx
Stack in Data Structure
Data Structures And Algorithms(stacks queues)
stack and types of stack with algorithms and stack
Stack and its Applications : Data Structures ADT
Unit I.pptx
Stack - Data Structure - Notes
Project on stack Data structure
Ad

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Spectroscopy.pptx food analysis technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
Teaching material agriculture food technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Cloud computing and distributed systems.
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Chapter 3 Spatial Domain Image Processing.pdf
The AUB Centre for AI in Media Proposal.docx
Digital-Transformation-Roadmap-for-Companies.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
MYSQL Presentation for SQL database connectivity
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectroscopy.pptx food analysis technology
Per capita expenditure prediction using model stacking based on satellite ima...
Understanding_Digital_Forensics_Presentation.pptx
Teaching material agriculture food technology
Programs and apps: productivity, graphics, security and other tools
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Spectral efficient network and resource selection model in 5G networks
Cloud computing and distributed systems.
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
sap open course for s4hana steps from ECC to s4
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

Auckland Programming Algorithms and Performance Meetup about Stacks & LeetCode Problems

  • 1. Stacks Rodrigo Villarreal && Paulo Almeida Auckland Data Structures and Algorithms
  • 2. Agenda ● Introduction to Stacks ○ Design ○ Real-world applications ● Practice ○ Examples ○ Let’s get our hands dirty! ● What’s next?
  • 3. What is a Stack ? A stack is an abstract data type that serves as a collection of elements, with two principal operations: 1 1 2 3 5 6bottom top
  • 4. What is a Stack ? A stack is an abstract data type that serves as a collection of elements, with two principal operations: 1 1 2 3 5 6 pop()bottom
  • 5. What is a Stack ? A stack is an abstract data type that serves as a collection of elements, with two principal operations: 1 1 2 3 5 8 push(x) bottom
  • 6. What is a Stack ? A stack is an abstract data type that serves as a collection of elements, with two principal operations: 1 1 2 3 5 8 Both operations change the top of the stack. bottom top
  • 7. What is a Stack ? This behaviour is sometimes referred as LIFO (Last in, First out).
  • 8. What is a Stack ? This behaviour is sometimes referred to as LIFO (Last in, First out). 1 push(x) bottom top
  • 9. What is a Stack ? This behaviour is sometimes referred to as LIFO (Last in, First out). 1 push(x) 1 bottom
  • 10. What is a Stack ? This behaviour is sometimes referred to as LIFO (Last in, First out). 1 push(x) 2 1bottom
  • 11. What is a Stack ? This behaviour is sometimes referred to as LIFO (Last in, First out). 1 push(x) 3 1 2bottom
  • 12. What is a Stack ? This behaviour is sometimes referred to as LIFO (Last in, First out). 1 31 2bottom top
  • 13. What is a Stack ? This behaviour is sometimes referred to as LIFO (Last in, First out). 1 pop() 3 1 2bottom
  • 14. What is a Stack ? This behaviour is sometimes referred to as LIFO (Last in, First out). 1 pop() 2 1bottom
  • 15. What is a Stack ? This behaviour is sometimes referred to as LIFO (Last in, First out). 1 pop() 1 bottom
  • 16. What is a Stack ? This behaviour is sometimes referred to as LIFO (Last in, First out). 1 pop() bottom top
  • 17. What is a Stack ? Like in a stack of books. One can only add or remove from the top of the stack. …. (Or push/pop respectively)
  • 18. Big O Time Complexity (Avg/Worst) Search: O(n) Access: O(n) Insertion: O(1) Deletion: O(1) Space Complexity (Worst): O(n)
  • 19. Where would I use Stacks in the real world ? I’m glad you asked :)
  • 20. Real life examples Parsing mathematical expressions String expr = ( 9 + ( 4 / 2 ) ) * 2
  • 21. Real life examples Parsing mathematical expressions String expr = ( push(x) bottom top ( 9 + ( 4 / 2 ) ) * 2
  • 22. String expr = (bottom top ( 9 + ( 4 / 2 ) ) * 2 Real life examples Parsing mathematical expressions
  • 23. String expr = (bottom push(x) 9 ( 9 + ( 4 / 2 ) ) * 2 Real life examples Parsing mathematical expressions
  • 24. String expr = (bottom 9 ( 9 + ( 4 / 2 ) ) * 2 Real life examples Parsing mathematical expressions
  • 25. String expr = (bottom 9 push(x) + ( 9 + ( 4 / 2 ) ) * 2 Real life examples Parsing mathematical expressions
  • 26. String expr = (bottom 9 + ( 9 + ( 4 / 2 ) ) * 2 Real life examples Parsing mathematical expressions
  • 27. String expr = (bottom 9 + push(x) ( ( 9 + ( 4 / 2 ) ) * 2 Real life examples Parsing mathematical expressions
  • 28. String expr = (bottom 9 + ( ( 9 + ( 4 / 2 ) ) * 2 Real life examples Parsing mathematical expressions
  • 29. String expr = (bottom 9 + ( push(x) 4 ( 9 + ( 4 / 2 ) ) * 2 Real life examples Parsing mathematical expressions
  • 30. String expr = (bottom 9 + ( 4 ( 9 + ( 4 / 2 ) ) * 2 Real life examples Parsing mathematical expressions
  • 31. String expr = (bottom 9 + ( 4 ( 9 + ( 4 / 2 ) ) * 2 push(x) / Real life examples Parsing mathematical expressions
  • 32. String expr = (bottom 9 + ( 4 ( 9 + ( 4 / 2 ) ) * 2 / Real life examples Parsing mathematical expressions
  • 33. String expr = (bottom 9 + ( 4 ( 9 + ( 4 / 2 ) ) * 2 / push(x) 2 Real life examples Parsing mathematical expressions
  • 34. String expr = (bottom 9 + ( 4 ( 9 + ( 4 / 2 ) ) * 2 / 2 Real life examples Parsing mathematical expressions
  • 35. String expr = (bottom 9 + ( 4 ( 9 + ( 4 / 2 ) ) * 2 / 2 pop()pop()pop()pop() Real life examples Parsing mathematical expressions
  • 36. String expr = (bottom 9 + ( 9 + ( 4 / 2 ) ) * 2 push(x) 2 Real life examples Parsing mathematical expressions
  • 37. String expr = (bottom 9 + ( 9 + ( 4 / 2 ) ) * 2 2 Real life examples Parsing mathematical expressions
  • 38. String expr = (bottom 9 + ( 9 + ( 4 / 2 ) ) * 2 2 pop()pop()pop()pop() Real life examples Parsing mathematical expressions
  • 39. String expr = bottom ( 9 + ( 4 / 2 ) ) * 2 11 push(x) top Real life examples Parsing mathematical expressions
  • 40. String expr = bottom ( 9 + ( 4 / 2 ) ) * 2 11 top Real life examples Parsing mathematical expressions
  • 41. String expr = bottom ( 9 + ( 4 / 2 ) ) * 2 11 push(x) * Real life examples Parsing mathematical expressions
  • 42. String expr = bottom ( 9 + ( 4 / 2 ) ) * 2 11 * Real life examples Parsing mathematical expressions
  • 43. String expr = bottom ( 9 + ( 4 / 2 ) ) * 2 11 * push(x) 2 Real life examples Parsing mathematical expressions
  • 44. String expr = bottom ( 9 + ( 4 / 2 ) ) * 2 11 * 2 Real life examples Parsing mathematical expressions
  • 45. String expr = bottom ( 9 + ( 4 / 2 ) ) * 2 11 * 2 pop()pop()pop() Real life examples Parsing mathematical expressions
  • 46. String expr = ( 9 + ( 4 / 2 ) ) * 2 bottom 22 push(x) top Real life examples Parsing mathematical expressions
  • 47. Using Stacks in Java Declare stack: Stack<Type> stack = new Stack<>(); Push element: stack.push(element); Pop element: stack.pop();
  • 48. Valid Parentheses (L #20) Given a string containing just the characters determine if the input string is valid. An input string is valid if: ● Open brackets must be closed by the same type of brackets. ● Open brackets must be closed in the correct order. ( ) { } [ ]
  • 49. Valid Parentheses (L #20) Test cases Example Output “()” true “()[]{}” true “(]” false “([)]” false “{[]}” true
  • 52. Valid Parentheses (L #20) Time Complexity = O(n) Space Complexity = O(n)
  • 53. Still not convinced… where else would I use stacks? Can you relate to this example?
  • 54. Real life examples Back / Forward Browser Button Back Stack Forward Stack
  • 55. Real life examples Back / Forward Browser Button Back Stack Forward Stack www.google.comcurrent
  • 56. Real life examples Back / Forward Browser Button Back Stack Forward Stack www.google.com www.facebook.comcurrent
  • 57. Real life examples Back / Forward Browser Button Back Stack Forward Stack www.google.com www.facebook.com current www.twitter.com
  • 58. Real life examples Back / Forward Browser Button Back Stack Forward Stack www.google.com www.facebook.com current www.twitter.com www.meetup.com
  • 59. Real life examples Back / Forward Browser Button Back Stack Forward Stack www.google.com www.facebook.com current www.twitter.com www.meetup.com www.linkedin.com
  • 60. Real life examples Back / Forward Browser Button Back Stack Forward Stack www.google.com www.facebook.com current www.twitter.com www.meetup.com www.linkedin.com
  • 61. Real life examples Back / Forward Browser Button Back Stack Forward Stack www.google.com www.facebook.com current www.twitter.com www.meetup.com www.linkedin.com
  • 62. Real life examples Back / Forward Browser Button
  • 63. Real life examples Back / Forward Browser Button What happens if I write a new URL while I am in Twitter? Back Stack Forward Stack www.google.com www.facebook.com current www.twitter.com www.meetup.com www.linkedin.com
  • 64. Real life examples Back / Forward Browser Button Forward Stack gets cleared Back Stack Forward Stack www.google.com www.facebook.com current www.twitter.com www.gmail.com
  • 65. Remove All Adjacent Duplicates In String (L #1047) Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them. We repeatedly make duplicate removals on S until we no longer can. Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique. Input: "abbaca" Output: "ca" Explanation: For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
  • 66. Remove All Adjacent Duplicates In String (L #1047) Test cases Input: "abbaca" Output: "ca" Input: "abcdeedcba" Output: "" Input: "abcdefg" Output: "abcdefg"
  • 67. Remove All Adjacent Duplicates In String (L #1047) Approach #1 Iterate the string and remove the adjacents. If we find a pair, we set a flag to true indicating we need to iterate again (since we have created a new string) Input: "abcdeedcba" Iteration #1: "abcddcba" Iteration #5: "abba" Iteration #2: "abccba" Iteration #6: "aa" Iteration #3: "abccba" Iteration #7 : "" Iteration #4: "abccba"
  • 68. Remove All Adjacent Duplicates In String (L #1047) Approach #1 Time Complexity = O(n^2) Space Complexity = O(n)
  • 69. Remove All Adjacent Duplicates In String (L #1047) Approach #2 We’ll use a stack this time as our support tool to solve this problem. We’ll iterate the string and we’ll check if the stack has elements and if top of the stack (the previous char) is equal to the element we’re currently checking. If the previous statement is false, add the char to the stack If the previous statement is true, pop the previous char and don’t add the current to the stack
  • 70. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack
  • 71. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack
  • 72. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack a
  • 73. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack a
  • 74. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack a b
  • 75. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack a b
  • 76. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack a
  • 77. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack a
  • 78. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack
  • 79. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack
  • 80. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack c
  • 81. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack c
  • 82. Remove All Adjacent Duplicates In String (L #1047) a b b a c a Input Stack c a
  • 83. Remove All Adjacent Duplicates In String (L #1047) Final Result c a
  • 84. Remove All Adjacent Duplicates In String (L #1047) Approach #1 Time Complexity = O(n^2) Space Complexity = O(n) Approach #2 Time Complexity = O(n) Space Complexity = O(n)
  • 85. Remove All Adjacent Duplicates In String (L #1047) Approach #1 Time Complexity = O(n^2) Space Complexity = O(n) Approach #2 ← Winner! Time Complexity = O(n) Space Complexity = O(n)
  • 87. Leetcode Problems Easy Remove All Adjacent Duplicates In String (Leetcode #1047) Valid Parentheses (Leetcode #20) Min Stack (Leetcode #155) Decode String (Leetcode #394) Medium Design Browser History (Leetcode #1472) Remove K Digits (Leetcode #402) Online Stock Span (Leetcode #901) Hard Basic Calculator (Leetcode #224)
  • 88. What next? The next session will be about Trees!