Category: Stack

Find minimum value expression by inserting addition or multiplication operator between digits of given number

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”, […]

Continue Reading

Lexicographically largest subsequence containing all distinct characters only once

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 = […]

Continue Reading

Print all the paths from root to leaf, with a specified sum in Binary tree

  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 […]

Continue Reading

Construct a Binary Tree from String with bracket representation | Set 2

  #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] – ‘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* […]

Continue Reading

Reversing a Stack with the help of another empty Stack

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 […]

Continue Reading

Minimize length of a string by removing pairs of consecutive increasing or decreasing digits

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: […]

Continue Reading

Minimize length of a string by removing pairs of consecutive increasing or decreasing digits

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: […]

Continue Reading