📌  相关文章
📜  通过删除成对的连续递增或递减数字来最小化字符串的长度

📅  最后修改于: 2021-09-03 04:07:31             🧑  作者: Mango

给定一个由N 个数字组成的数字字符串S ,任务是找到 可以通过重复删除以递增或递减顺序排列的相邻连续字符对而形成的字符串的最小长度。

例子:

方法:可以使用堆栈数据结构解决给定的问题。请按照以下步骤解决问题:

  • 初始化一个堆栈,说给定的字符串存储字符。
  • 遍历给定的字符串S并执行以下步骤:
    • 如果堆栈St为空,则将字符S[i]压入堆栈St
    • 如果栈顶字符即St.top()S[i] 的绝对值1 ,则从栈中弹出该元素。否则,将字符S[i]压入堆栈St
  • 完成上述步骤后,打印栈大小St作为结果。

下面是这个方法的实现:

C++
// C++ program for the above approach
 
#include 
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 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 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 st = new Stack();
 
    // 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


输出:
1

时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live