📌  相关文章
📜  通过去除另一个字符串的所有出现最小化的字符串

📅  最后修改于: 2021-09-06 05:24:12             🧑  作者: Mango

给定两个长度分别为NM 的字符串S1S2 由小写字母组成,任务是通过从字符串S1 中删除所有出现的字符串S2来找到可以将S1减少到的最小长度。

例子:

方法:解决这个问题的思路是使用Stack Data Structure。请按照以下步骤解决给定的问题:

  • 初始化一个堆栈,将字符串S1的字符存储在其中。
  • 遍历给定的字符串S1并执行以下操作:
    • 将当前字符压入堆栈。
    • 如果堆栈的大小至少为 M ,则检查堆栈的顶部M 个字符是否形成字符串S2 。如果发现是真的,则删除那些字符。
  • 完成上述步骤后,剩余的堆栈大小就是所需的最小长度。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
 
using namespace std;
 
// Function to find the minimum length
// to which string str can be reduced to
// by removing all occurences of string K
int minLength(string str, int N,
              string K, int M)
{
 
    // Initialize stack of characters
    stack stackOfChar;
 
    for (int i = 0; i < N; i++) {
 
        // Push character into the stack
        stackOfChar.push(str[i]);
 
        // If stack size >= K.size()
        if (stackOfChar.size() >= M) {
 
            // Create empty string to
            // store characters of stack
            string l = "";
 
            // Traverse the string K in reverse
            for (int j = M - 1; j >= 0; j--) {
 
                // If any of the characters
                // differ, it means that K
                // is not present in the stack
                if (K[j] != stackOfChar.top()) {
 
                    // Push the elements
                    // back into the stack
                    int f = 0;
                    while (f != l.size()) {
 
                        stackOfChar.push(l[f]);
                        f++;
                    }
 
                    break;
                }
 
                // Store the string
                else {
 
                    l = stackOfChar.top()
                        + l;
 
                    // Remove top element
                    stackOfChar.pop();
                }
            }
        }
    }
 
    // Size of stack gives the
    // minimized length of str
    return stackOfChar.size();
}
 
// Driver Code
int main()
{
    string S1 = "fffoxoxoxfxo";
    string S2 = "fox";
 
    int N = S1.length();
    int M = S2.length();
 
    // Function Call
    cout << minLength(S1, N, S2, M);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find the minimum length
// to which String str can be reduced to
// by removing all occurences of String K
static int minLength(String str, int N,
              String K, int M)
{
 
    // Initialize stack of characters
    Stack stackOfChar = new Stack();
 
    for (int i = 0; i < N; i++)
    {
 
        // Push character into the stack
        stackOfChar.add(str.charAt(i));
 
        // If stack size >= K.size()
        if (stackOfChar.size() >= M)
        {
 
            // Create empty String to
            // store characters of stack
            String l = "";
 
            // Traverse the String K in reverse
            for (int j = M - 1; j >= 0; j--)
            {
 
                // If any of the characters
                // differ, it means that K
                // is not present in the stack
                if (K.charAt(j) != stackOfChar.peek())
                {
 
                    // Push the elements
                    // back into the stack
                    int f = 0;
                    while (f != l.length())
                    {
                        stackOfChar.add(l.charAt(f));
                        f++;
                    }
 
                    break;
                }
 
                // Store the String
                else
                {
                    l = stackOfChar.peek()
                        + l;
 
                    // Remove top element
                    stackOfChar.pop();
                }
            }
        }
    }
 
    // Size of stack gives the
    // minimized length of str
    return stackOfChar.size();
}
 
// Driver Code
public static void main(String[] args)
{
    String S1 = "fffoxoxoxfxo";
    String S2 = "fox";
 
    int N = S1.length();
    int M = S2.length();
 
    // Function Call
    System.out.print(minLength(S1, N, S2, M));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find the minimum length
# to which string str can be reduced to
# by removing all occurences of string K
def minLength(Str, N, K, M) :
 
    # Initialize stack of characters
    stackOfChar = []
 
    for i in range(N) :
 
        # Push character into the stack
        stackOfChar.append(Str[i])
 
        # If stack size >= K.size()
        if (len(stackOfChar) >= M) :
 
            # Create empty string to
            # store characters of stack
            l = ""
 
            # Traverse the string K in reverse
            for j in range(M - 1, -1, -1) :
 
                # If any of the characters
                # differ, it means that K
                # is not present in the stack
                if (K[j] != stackOfChar[-1]) :
 
                    # Push the elements
                    # back into the stack
                    f = 0
                    while (f != len(l)) :
                        stackOfChar.append(l[f])
                        f += 1
 
                    break
 
                # Store the string
                else :
                    l = stackOfChar[-1] + l
 
                    # Remove top element
                    stackOfChar.pop()
 
    # Size of stack gives the
    # minimized length of str
    return len(stackOfChar)
 
# Driver code 
S1 = "fffoxoxoxfxo"
S2 = "fox"
 
N = len(S1)
M = len(S2)
 
# Function Call
print(minLength(S1, N, S2, M))
 
# This code is contributed by divyeshrabadiya07


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
// Function to find the minimum length
// to which String str can be reduced to
// by removing all occurences of String K
static int minLength(String str, int N,
              String K, int M)
{
 
    // Initialize stack of characters
    Stack stackOfChar = new Stack();
    for (int i = 0; i < N; i++)
    {
 
        // Push character into the stack
        stackOfChar.Push(str[i]);
 
        // If stack size >= K.Count
        if (stackOfChar.Count >= M)
        {
 
            // Create empty String to
            // store characters of stack
            String l = "";
 
            // Traverse the String K in reverse
            for (int j = M - 1; j >= 0; j--)
            {
 
                // If any of the characters
                // differ, it means that K
                // is not present in the stack
                if (K[j] != stackOfChar.Peek())
                {
 
                    // Push the elements
                    // back into the stack
                    int f = 0;
                    while (f != l.Length)
                    {
                        stackOfChar.Push(l[f]);
                        f++;
                    }
                    break;
                }
 
                // Store the String
                else
                {
                    l = stackOfChar.Peek()
                        + l;
 
                    // Remove top element
                    stackOfChar.Pop();
                }
            }
        }
    }
 
    // Size of stack gives the
    // minimized length of str
    return stackOfChar.Count;
}
 
// Driver Code
public static void Main(String[] args)
{
    String S1 = "fffoxoxoxfxo";
    String S2 = "fox";
 
    int N = S1.Length;
    int M = S2.Length;
 
    // Function Call
    Console.Write(minLength(S1, N, S2, M));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3

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

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