📜  通过从字符串删除另一个字符串作为子字符串的出现来最小化字符串的长度

📅  最后修改于: 2021-05-04 15:40:45             🧑  作者: Mango

给定一个字符串S和一个字符串T ,任务是找到在删除所有可能出现的字符串T作为字符串S中的子字符串之后,将字符串S减少到的最小可能长度。

例子:

方法:我们的想法是迭代指定字符串的字符和初始化辅助字符串,并检查是否新组成的字符串呈现为给定的字符串的子串。如果发现是真的,那么只需从给定的字符串删除该子字符串。

请按照以下步骤解决此问题:

  1. 首先,通过遍历字符串并跟踪遇到的字符来标识子字符串T。
  2. 但是,当删除子字符串时,其余部分的连接非常昂贵,因为每个字符都必须向后移动M个位置。
  3. 为了避免这种情况,请维护一个名为temp的字符串,该字符串仅包含到目前为止已迭代的字符。
  4. 因此,如果所需的子字符串存在于temp中,则只需在恒定的计算时间内删除最后的M个字符。
  5. 最后,执行所有操作后,打印最小长度的字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to minimize length of
// string S after removing all
// occurrences of string T as substring
void minLength(string& S, string& T,
               int N, int M)
{
    string temp;
 
    // Count of characters
    // required to be removed
    int subtract = 0;
 
    // Iterate over the string
    for (int i = 0; i < N; ++i) {
 
        // Insert the current
        // character to temp
        temp.push_back(S[i]);
 
        // Check if the last M
        // characters forms t or not
        if (temp.size() >= M) {
 
            // Getting the last M
            // characters. If they
            // are equal to t then
            // remove the last M
            // characters from the temp string
            if (temp.substr(temp.size() - M, M) == T) {
 
                // Incrementing subtract by M
                subtract += M;
 
                // Removing last M
                // characters from the
                // string
                int cnt = 0;
                while (cnt != M) {
                    temp.pop_back();
                    ++cnt;
                }
            }
        }
    }
 
    // Print the final answer
    cout << (N - subtract) << "\n";
}
 
// Driver Code
int main()
{
    // Given string S & T
    string S = "aabcbcbd", T = "abc";
 
    // Length of string S
    int N = S.size();
 
    // Length of string T
    int M = T.size();
 
    // Prints the count of
    // operations required
    minLength(S, T, N, M);
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to minimize length of
// string S after removing all
// occurrences of string T as substring
static void minLength(String S, String T,
                      int N, int M)
{
    String temp = "";
     
    // Count of characters
    // required to be removed
    int subtract = 0;
 
    // Iterate over the string
    for(int i = 0; i < N; ++i)
    {
         
        // Insert the current
        // character to temp
        temp += S.charAt(i);
 
        // Check if the last M
        // characters forms t or not
        if (temp.length() >= M)
        {
             
            // Getting the last M characters.
            // If they are equal to t then
            // remove the last M characters
            // from the temp string
            if (T.equals(
                temp.substring(temp.length() - M,
                               temp.length())))
            {
                 
                // Incrementing subtract by M
                subtract += M;
 
                // Removing last M
                // characters from the
                // string
                int cnt = 0;
                while (cnt != M)
                {
                    temp = temp.substring(
                        0, temp.length() - 1);
                    ++cnt;
                }
            }
        }
    }
     
    // Print the final answer
    System.out.println((N - subtract));
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given string S & T
    String S = "aabcbcbd", T = "abc";
     
    // Length of string S
    int N = S.length();
 
    // Length of string T
    int M = T.length();
 
    // Prints the count of
    // operations required
    minLength(S, T, N, M);
}
}
 
// This code is contributed by Dharanendra L V


Python3
# Python program for the above approach
 
# Function to minimize length of
# string S after removing all
# occurrences of string T as substring
def minLength(S, T, N, M):
    temp = "";
 
    # Count of characters
    # required to be removed
    subtract = 0;
 
    # Iterate over the string
    for i in range(N):
 
        # Insert the current
        # character to temp
        temp += S[i];
 
        # Check if the last M
        # characters forms t or not
        if (len(temp) >= M):
 
            # Getting the last M characters.
            # If they are equal to t then
            # remove the last M characters
            # from the temp string
            if (T ==(temp[len(temp) - M: len(temp)])):
 
                # Incrementing subtract by M
                subtract += M;
 
                # Removing last M
                # characters from the
                # string
                cnt = 0;
                while (cnt != M):
                    temp = temp[0: len(temp) - 1];
                    cnt+= 1;
 
    # Prthe final answer
    print((N - subtract));
 
# Driver Code
if __name__ == '__main__':
   
    # Given string S & T
    S = "aabcbcbd";
    T = "abc";
 
    # Length of string S
    N = len(S);
 
    # Length of string T
    M = len(T);
 
    # Prints the count of
    # operations required
    minLength(S, T, N, M);
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to minimize length of
// string S after removing all
// occurrences of string T as substring
static void minLength(String S, String T,
                      int N, int M)
{
    String temp = "";
     
    // Count of characters
    // required to be removed
    int subtract = 0;
 
    // Iterate over the string
    for(int i = 0; i < N; ++i)
    {
         
        // Insert the current
        // character to temp
        temp += S[i];
 
        // Check if the last M
        // characters forms t or not
        if (temp.Length >= M)
        {
             
            // Getting the last M characters.
            // If they are equal to t then
            // remove the last M characters
            // from the temp string
            if (T.Equals(
                temp.Substring(temp.Length - M, M)))
            {
                 
                // Incrementing subtract by M
                subtract += M;
 
                // Removing last M
                // characters from the
                // string
                int cnt = 0;
                 
                while (cnt != M)
                {
                    temp = temp.Substring(
                        0, temp.Length - 1);
                    ++cnt;
                }
            }
        }
    }
     
    // Print the readonly answer
    Console.WriteLine((N - subtract));
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given string S & T
    String S = "aabcbcbd", T = "abc";
     
    // Length of string S
    int N = S.Length;
 
    // Length of string T
    int M = T.Length;
 
    // Prints the count of
    // operations required
    minLength(S, T, N, M);
}
}
 
// This code is contributed by shikhasingrajput


输出:
2

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