📌  相关文章
📜  通过重复删除相同字符的偶数长度子字符串来最小化二进制字符串

📅  最后修改于: 2021-09-04 08:00:21             🧑  作者: Mango

给定一个大小为N的二进制字符串str ,任务是通过从字符串中多次删除由 sam字符组成的偶数长度子字符串(即仅0 s 或1 s)来最小化给定二进制字符串的长度。最后,打印修改后的字符串。

例子:

方法:思路是使用堆栈来解决问题。在遍历字符串,如果发现当前字符与栈顶元素相同,则将该元素从栈中弹出。遍历后,从下到上打印堆栈。请按照以下步骤解决问题:

  • 声明一个堆栈并将字符串str的第一个字符压入堆栈。
  • 使用变量i在索引范围[1, N – 1] 上遍历字符串str
    • 如果堆栈为空,则将字符str[i]压入堆栈。
    • 否则,检查str[i]是否等于栈顶。如果发现为真,则将其从堆栈中弹出。否则,将字符str[i]推送给它。
  • 完成上述步骤后,从下到上打印堆栈。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Recursive function to print stack
// elements from bottom to top without
// changing their order
void PrintStack(stack s)
{
    // If stack is empty
    if (s.empty())
        return;
 
    char x = s.top();
 
    // Pop top element of the stack
    s.pop();
 
    // Recursively call the
    // function PrintStack
    PrintStack(s);
 
    // Print the stack element
    // from the bottom
    cout << x;
 
    // Push the same element onto the
    // stack to preserve the order
    s.push(x);
}
 
// Function to minimize binary string
// by removing substrings consisting
// of same character
void minString(string s)
{
    // Declare a stack of characters
    stack Stack;
 
    // Push the first character of
    // the string into the stack
    Stack.push(s[0]);
 
    // Traverse the string s
    for (int i = 1; i < s.size(); i++) {
 
        // If Stack is empty
        if (Stack.empty()) {
 
            // Push current character
            // into the stack
            Stack.push(s[i]);
        }
 
        else {
 
            // Check if the current
            // character is same as
            // the top of the stack
            if (Stack.top() == s[i]) {
 
                // If true, pop the
                // top of the stack
                Stack.pop();
            }
 
            // Otherwise, push the
            // current element
            else {
                Stack.push(s[i]);
            }
        }
    }
 
    // Print stack from bottom to top
    PrintStack(Stack);
}
 
// Driver Code
int main()
{
    string str = "101001";
    minString(str);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Recursive function to print stack
// elements from bottom to top without
// changing their order
static void PrintStack(Stack s)
{
    // If stack is empty
    if (s.isEmpty())
        return;
 
    char x = s.peek();
 
    // Pop top element of the stack
    s.pop();
 
    // Recursively call the
    // function PrintStack
    PrintStack(s);
 
    // Print the stack element
    // from the bottom
    System.out.print(x);
 
    // Push the same element onto the
    // stack to preserve the order
    s.add(x);
}
 
// Function to minimize binary String
// by removing subStrings consisting
// of same character
static void minString(String s)
{
    // Declare a stack of characters
    Stack Stack = new Stack();
 
    // Push the first character of
    // the String into the stack
    Stack.add(s.charAt(0));
 
    // Traverse the String s
    for (int i = 1; i < s.length(); i++) {
 
        // If Stack is empty
        if (Stack.isEmpty()) {
 
            // Push current character
            // into the stack
            Stack.add(s.charAt(i));
        }
 
        else {
 
            // Check if the current
            // character is same as
            // the top of the stack
            if (Stack.peek() == s.charAt(i)) {
 
                // If true, pop the
                // top of the stack
                Stack.pop();
            }
 
            // Otherwise, push the
            // current element
            else {
                Stack.push(s.charAt(i));
            }
        }
    }
 
    // Print stack from bottom to top
    PrintStack(Stack);
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "101001";
    minString(str);
 
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Recursive function to print stack
# elements from bottom to top without
# changing their order
def PrintStack(s) :
 
    # If stack is empty
    if (len(s) == 0) :
        return;
 
    x = s[-1];
 
    # Pop top element of the stack
    s.pop();
 
    # Recursively call the
    # function PrintStack
    PrintStack(s);
 
    # Print the stack element
    # from the bottom
    print(x, end="");
 
    # Push the same element onto the
    # stack to preserve the order
    s.append(x);
 
# Function to minimize binary string
# by removing substrings consisting
# of same character
def minString(s) :
 
    # Declare a stack of characters
    Stack = [];
 
    # Push the first character of
    # the string into the stack
    Stack.append(s[0]);
 
    # Traverse the string s
    for i in range(1, len(s)) :
 
        # If Stack is empty
        if (len(Stack) == 0) :
 
            # Push current character
            # into the stack
            Stack.append(s[i]);
 
        else:
 
            # Check if the current
            # character is same as
            # the top of the stack
            if (Stack[-1] == s[i]) :
 
                # If true, pop the
                # top of the stack
                Stack.pop();
 
            # Otherwise, push the
            # current element
            else :
                Stack.append(s[i]);
 
    # Print stack from bottom to top
    PrintStack(Stack);
 
# Driver Code
if __name__ == "__main__" :
 
    string = "101001";
    minString(string);
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
// Recursive function to print stack
// elements from bottom to top without
// changing their order
static void PrintStack(Stack s)
{
    // If stack is empty
    if (s.Count == 0)
        return;
    char x = s.Peek();
 
    // Pop top element of the stack
    s.Pop();
 
    // Recursively call the
    // function PrintStack
    PrintStack(s);
 
    // Print the stack element
    // from the bottom
    Console.Write((char)x);
 
    // Push the same element onto the
    // stack to preserve the order
    s.Push(x);
}
 
// Function to minimize binary String
// by removing subStrings consisting
// of same character
static void minString(String s)
{
    // Declare a stack of characters
    Stack Stack = new Stack();
 
    // Push the first character of
    // the String into the stack
    Stack.Push(s[0]);
 
    // Traverse the String s
    for (int i = 1; i < s.Length; i++)
    {
 
        // If Stack is empty
        if (Stack.Count == 0)
        {
 
            // Push current character
            // into the stack
            Stack.Push(s[i]);
        }
 
        else
        {
 
            // Check if the current
            // character is same as
            // the top of the stack
            if (Stack.Peek() == s[i])
            {
 
                // If true, pop the
                // top of the stack
                Stack.Pop();
            }
 
            // Otherwise, push the
            // current element
            else
            {
                Stack.Push(s[i]);
            }
        }
    }
 
    // Print stack from bottom to top
    PrintStack(Stack);
}
 
// Driver Code
public static void Main(String[] args)
{
    String str = "101001";
    minString(str);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
10

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

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