# 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: 1Explanation: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: 4Approach: The given problem can be solved using the Stack Data Structure. Follow the steps below to solve the problem:Below is the implementation of this approach:C++#include using namespace std;int minLength(string S){        stack st;        for (auto ch : S) {                if (st.empty())            st.push(ch);                else {                                    char top = st.top();                                    if (abs(ch – top) == 1)                st.pop();                                    else {                st.push(ch);            }        }    }        return st.size();}int main(){    string S = “12213”;    cout