给定一个字符串S和一个字符串T ,任务是在删除所有可能出现的字符串T作为字符串S 中的子字符串后,找到字符串S可以减少到的最小可能长度。
例子:
Input: S = “aabcbcbd”, T = “abc”
Output: 2
Explanation:
Removing the substring {S[1], …, S[3]} and modifies the remaining string to “abcbd”.
Removing the substring {S[0] .. S[2]}, the resultant string modifies to “bd”.
Therefore, the required answer is 2.
Input: S = “asdfbc”, T = “xyz”
Output: 0
Explanation:
No occurrence of the string “xyz” as a substring in S.
方法:我们的想法是迭代指定字符串的字符和初始化辅助字符串,并检查是否新组成的字符串呈现为给定的字符串的子串。如果发现为真,则只需从给定的字符串删除该子字符串。
请按照以下步骤解决此问题:
- 首先,通过遍历字符串并跟踪遇到的字符来识别子字符串T。
- 但是,当删除子字符串时,剩余部分的连接成本很高,因为每个字符都必须向后移动M位。
- 为了避免这种情况,请维护一个字符串,比如temp ,它只包含到目前为止迭代的字符。
- 因此,如果所需的子字符串存在于temp 中,则只需在恒定计算时间内删除最后M 个字符。
- 最后,在执行所有操作后打印字符串的最小长度。
下面是上述方法的实现:
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
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live