Monday, 29 Nov 2021
Category: Stack

Given string str that contains only digits, the task is to return an expression by inserting the ‘+’ or ‘*’ operator between every two digits such that the arithmetic value of the expression is minimized.Example: Input: str = “322”Output: “3+2*2”Explanation: The value of above expression is 7 which is minimum possible over the other expressions “3+2+2”, […]

Given a string S, the task is to find the lexicographically largest subsequence that can be formed using all distinct characters only once from the given string.Examples:Input: S = ababcOutput: bacExplanation:All possible subsequences containing all the characters in S exactly once are {“abc”, “bac”}. The lexicograohically maximum among all the subsequences  is “bac”.Input: S = […]

import java.util.*;  class GFG {      static List result        = new ArrayList();          static class Node {        int data;        Node left, right;    };          static Node newNode(int data)    {        Node temp = new Node();        temp.data = data;        temp.left = temp.right = null;        return temp;    }              static void pathSumUtil(        Node node, int targetSum,        Stack stack)    {        if (node == null) {            return;        }        stack.push(node.data);          if (node.left == null            && node.right == null) {            if (node.data == targetSum) {                result.add(new ArrayList(stack));            }        }        pathSumUtil(node.left,                    targetSum – node.data,                    stack);        pathSumUtil(node.right,                    targetSum – node.data,                    stack);        stack.pop();    }              static List […]

#include using namespace std;  class Stack {private:            int Max = 5;              int* arr = new int(Max);              int minEle = 0;              int top = 0;  public:            bool empty()    {        if (top

#include using namespace std;  struct Node {    Node* left;    Node* right;    int data;              Node(int element)    {        data = element;        this->left = nullptr;        this->right = nullptr;    }};  void preorder(Node* root){    if (!root)        return;      cout data left);    preorder(root->right);}  Node* constructTree(string s){          Node* root = new Node(s – ‘0’);              stack stk;          for (int i = 1; i < s.length(); i++) {                  if (s[i] == '(') {                          stk.push(root);        }                  else if (s[i] == ')') {                                      root = stk.top();                          stk.pop();        }                  else {                                                  if (root->left == nullptr) {                  Node* […]

Program to insert an element at the Bottom of a StackGiven a stack S and an integer N, the task is to insert N at the bottom of the stack.Examples:Input: N = 7S = 1

Reversing a Stack with the help of another empty StackGiven a Stack consisting of N elements, the task is to reverse the Stack using an extra stack.Examples:Input: stack = {1, 2, 3, 4, 5}Output:12345Explanation:Input Stack:54321Reversed Stack:12345Input: stack = {1, 3, 5, 4, 2}Output:13542Approach: Follow the steps below to solve the problem:Below is the implementation of […]

Minimize length of a string by removing pairs of consecutive increasing or decreasing digitsGiven a numeric string S consisting of N digits, the task is to find the minimum length of the string that can be formed by repeatedly removing pairs of adjacent consecutive characters arranged in either increasing or decreasing order.Examples:Input: S = “12213”Output: […]

Minimize length of a string by removing pairs of consecutive increasing or decreasing digitsGiven a numeric string S consisting of N digits, the task is to find the minimum length of the string that can be formed by repeatedly removing pairs of adjacent consecutive characters arranged in either increasing or decreasing order.Examples:Input: S = “12213”Output: […]

Remove trailing zeros from the sum of two numbers ( Using Stack )Given two numbers A and B, the task is to remove the trailing zeros present in the sum of the two given numbers using a stack.Examples:Input: A = 124, B = 186Output: 31Explanation: Sum of A and B is 310. Removing the trailing […]