Minimize length of a string by removing pairs of consecutive increasing or decreasing digits
Given 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: 1
Explanation:
The minimum length of the string S that can be obtained by removing elements in following way:
- Remove substring {S[0], S[1]}. The string S modifies to “213”
- Remove substring {S[0], S[1]}. The string S modifies to “3”
Therefore, the length of the string S is 1, which is the minimum possible length.
Input: S = “1350”
Output: 4
Approach: The given problem can be solved using the Stack Data Structure. Follow the steps below to solve the problem:
- Initialize a stack, say St that stores the characters of the given string.
- Traverse the given string S and perform the following steps:
- If the stack St is empty then, push the character S[i] in the stack St.
- If the absolute value of the character at the top of the stack i.e., St.top() and S[i] is 1, then pop the element from the stack. Otherwise, push the character S[i] in the stack St.
- After completing the above steps, print the size of the stack St as the result.
Below is the implementation of this approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum length // of the string possible after removing // pairs of consecutive digits int minLength(string S) { // Initialize the stack st stack< char > st; // Traverse the string S for ( auto ch : S) { // If the stack is empty if (st.empty()) st.push(ch); // Otherwise else { // Get the top character // of the stack char top = st.top(); // If cha and top are // consecutive digits if ( abs (ch - top) == 1) st.pop(); // Otherwise, push the // character ch else { st.push(ch); } } } // Print the size of the stack return st.size(); } // Driver Code int main() { string S = "12213" ; cout << minLength(S); return 0; } |
Java
// java program for the above approach import java.io.*; import java.lang.*; import java.util.*; public class GFG { // Function to find the minimum length // of the string possible after removing // pairs of consecutive digits static int minLength(String S) { // Initialize the stack st Stack<Character> st = new Stack<>(); // Traverse the string S for ( char ch : S.toCharArray()) { // If the stack is empty if (st.isEmpty()) st.push(ch); // Otherwise else { // Get the top character // of the stack char top = st.peek(); // If cha and top are // consecutive digits if (Math.abs(ch - top) == 1 ) st.pop(); // Otherwise, push the // character ch else { st.push(ch); } } } // Print the size of the stack return st.size(); } // Driver Code public static void main(String[] args) { String S = "12213" ; System.out.println(minLength(S)); } } // This code is contributed by Kingash. |
Python3
# Python 3 program for the above approach # Function to find the minimum length # of the string possible after removing # pairs of consecutive digits def minLength(S): # Initialize the stack st st = [] # Traverse the string S for ch in S: # If the stack is empty if ( len (st) = = 0 ): st.append(ch) # Otherwise else : # Get the top character # of the stack top = st[ - 1 ] # If cha and top are # consecutive digits if ( abs ( ord (ch) - ord (top)) = = 1 ): st.pop() # Otherwise, push the # character ch else : st.append(ch) # Print the size of the stack return len (st) # Driver Code if __name__ = = "__main__" : S = "12213" print (minLength(S)) # This code is contributed by ukasp. |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to find the minimum length // of the string possible after removing // pairs of consecutive digits static int minLength(String S) { // Initialize the stack st Stack< char > st = new Stack< char >(); // Traverse the string S foreach ( char ch in S.ToCharArray()) { // If the stack is empty if (st.Count == 0) st.Push(ch); // Otherwise else { // Get the top character // of the stack char top = st.Peek(); // If cha and top are // consecutive digits if (Math.Abs(ch - top) == 1) st.Pop(); // Otherwise, push the // character ch else { st.Push(ch); } } } // Print the size of the stack return st.Count; } // Driver Code public static void Main(String[] args) { String S = "12213" ; Console.WriteLine(minLength(S)); } } // This code is contributed by shikhasingrajput |
Javascript
<script> // JavaScript program for the above approach // Function to find the minimum length // of the string possible after removing // pairs of consecutive digits function minLength(S) { // Initialize the stack st var st = []; // Traverse the string S S.split( '' ).forEach(ch => { // If the stack is empty if (st.length==0) st.push(ch); // Otherwise else { // Get the top character // of the stack var top =st[st.length-1]; // If cha and top are // consecutive digits if (Math.abs(ch - top) == 1) st.pop(); // Otherwise, push the // character ch else { st.push(ch); } } }); // Print the size of the stack return st.length; } // Driver Code var S = "12213" ; document.write( minLength(S)); </script> |
Output:
1
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...