Stacks: Validate stack operations

Problem Statement: You are given 2 sequence of operations, push and pop. You need to return true if it is result of a sequence of push and pop operations on an initially empty stack Example Input: push = [1,2,3,4,5], pop = [4,5,3,2,1] Output: True push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() -> 5, […]

Score of Parentheses

Problem Statement: You are given a string consisting of pair of balanced parentheses. You need to calculate the total score of the given string as below: “()” has a score of 1. “m n” has a score of m + n where m and n are individual pairs of balanced parentheses. “(m)” has a score […]

Stacks: Check if the given pattern exist in the array

Problem Statement: You are given an array, you need to find 3 integers, such that i < j < k and nums[i] < nums[k] < nums[j] Example Input: nums = [3,1,4,2] Output: True The sequence is: [1, 4, 2] Solution We will use stack for the solution. Basic Idea: The highest element will remain in […]

Stacks: Minimum Insertions to Balance a Parentheses String

Problem Statement: You are given a string with “(” and “)”. You need to make the string balanced and return the min count. We consider a string as balanced if for every ‘(‘, there is corresponding ‘))’ Example Input: s = “(()))” Output: 1 For the second ‘(‘ we have 2 ‘))’. But for the […]

Stack: Make digit minimum by removing k digits.

Problem Statement: You are given a string represents a integer and integer k. You need to remove k digits from the integer and it must be the smallest. Example Input: num = “1432219”, k = 3 Output: “1219” Remove 4, 3, 2 to make the digit smallest. Solution We will use stack to solve the […]

Stack: Build an Array With Stack Operations

Problem Statement: You are given an target and an integer n. You need to perform push and pop operation to reach the target by using the list from “1, 2, … n”. Example Input: target = [1,3], n = 3 Output: [“Push”,”Push”,”Pop”,”Push”] Read number 1 and do push operation. Current array [1] Then read number […]

Stack: Remove All Adjacent Duplicates In String

Problem Statement: Given a string, remove all adjecent duplicate characters. Example Input: s = “abbaca” Output: “ca” Solution You need to use a stack for the solution. Insert characters from the string one by one. Then if the next character is sa,e as the last character in res, then pop the last char form the […]

Stack: Remove outer most redundant parenthesis

Problem Statement: You are given a string “S”. You need to print the string obtained by removing the outermost parenthesis. Example Input: S = “(()())(())()” Output: ()()() Solution In the solution, we will use stack, We will take 2 variables, one stack and one resultant string. When we encounter “(“, we will check if the […]

Check if given 2 strings are same with backspace character

Problem Statement: You are given 2 strings, and there is a special character “#” that represents a backspace. When you encounter “#”, you have to delete the previous character. You need to check if the given 2 strings are same. Example Input: s = “ab#c”, t = “ad#c” Output: True after removing the backspace, both […]

Stack: Get the next greater element in a circular array

Problem Statement: You are given an integer array you need to return the next greater element. You can search circularly. Example Input: nums = [1,2,1] Output: [2,-1,2] For “1” the next greater element is 2. For “2” there is no next greater element “-1” For “1”, the next greater element is “2”, if you search […]