Table of contents
1.
Introduction
2.
Problem Statement
3.
Approach: Using Stack
3.1.
Code
3.2.
Complexity Analysis
3.2.1.
Time Complexity: O(N)
3.2.2.
Space Complexity: O(N)
4.
Frequently Asked Questions
4.1.
What is the time complexity of push() ,pop() and top() operations in stack ?
4.2.
Why the worst-case space complexity is O(N)?
5.
Conclusion
Last Updated: Mar 27, 2024
Hard

Minimize Length of a String by Removing Pairs of Consecutive Increasing or Decreasing Digits

Author Rhythm Jain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Stacks

Introduction

String questions are increasingly becoming popular among interview questions. Therefore it is necessary to master questions based on string applications to ace technical interviews with big companies.

So Today, we will discuss a problem based on the String and Its Applications.

Also see, Data Structures

Problem Statement

You are given a string S consisting of N digits. You aim to find the minimum length of string that can be formed after removing all the adjacent consecutive characters arranged in either increasing or decreasing order.

Example

Input:

S = “12213”

Output:

1

Explanation

Since substring ( S[0]: S[1] ) is in increasing order so remove it. The string becomes “213”. Now again, removing ( S[0]: S[1] ) because it’s decreasing consecutively. The new string becomes “3”. So the length of the string is 1.

Approach: Using Stack

The above problem can be easily solved by using a stack that can help us keep track of the characters of the string encountered so far.

Algorithm:

  • Initialize a stack of characters called “ stk ” that stores the characters of the given string S.
  • Now, traverse the String S and, in each iteration, perform the following steps-
    • If the “stk” is empty then, push the character S[i] in the stack “stk.”
    • Else If the current character S[i] and character at the top of the stack differ by 1 then, pop the element from the stack “stk.” Else, push the character S[i] in the stack “stk.”
  • Return the size of the stack “stk”.

Code

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int minLength(string S)
{
   // Initialize the stack st
   stack<char> stk;

   // Traverse the string S
   for (int i=0;i<S.length();i++) {

       // If the stack is empty
       if (stk.empty())
           stk.push(S[i]);

       // Otherwise
       else {

           // If current character and stk.top are
           // consecutive digits
           if (abs(S[i] - stk.top()) == 1)
               stk.pop();

           // Otherwise, push the
           // character ch
           else {
               stk.push(S[i]);
           }
       }
   }

   // Print the size of the stack
   return stk.size();
}

// Driver Code
int main()
{
   string S = "12213";
   cout << minLength(S);

   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Input:

“12213”

Output:

1

Complexity Analysis

Time Complexity: O(N)

This is because we are traversing the entire string once

Space Complexity: O(N)

The algorithm requires a stack to store elements/characters of the string. So in the worst case, we would need O(N) space.
Check out this problem - Longest String Chain

Frequently Asked Questions

What is the time complexity of push() ,pop() and top() operations in stack ?

In a stack, the complexity of push(), pop(), and top() operations depends upon the implementation of the stack. For the inbuilt STL Stack, the complexity of all the three functions, push(), pop(),.” and top() is O(1).

Why the worst-case space complexity is O(N)?

In the worst case, we mean that neither of the adjacent elements in the string is in a consecutive increasing or decreasing fashion, so in that case, all of the string elements will be pushed into the stack leading to O(N) space.

Conclusion

String and application of various data structures on strings are the most critical technical and coding interview concepts.

On the other hand, Stack seems pretty simple data structure but can help us solve various problems efficiently and quickly.

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, Basics of C++, Basics of Java, Basics of Python, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Cheers!

Live masterclass