给定两个长度分别为N和M 的字符串S和T ,任务是检查字符串S 是否可以通过删除字符串S 的至多一个子字符串来转换为字符串T。如果发现是真的,则打印“YES” 。否则,打印“NO” 。
例子:
Input: S = “abcdef”, T = “abc”
Output: YES
Explanation:
Removing the substring { S[3], …, S[5] } modifies S to “abc”.
Since the string S equal to T, the required output is “YES”.
Input: S = “aaabbb”, T = “ab”
Output: YES
Explanation:
Removing the substring { S[1], …, S[4] } modifies S to “ab”.
Since the string S equal to T, the required output is “YES”.
朴素的方法:解决这个问题的最简单的方法是生成字符串S 的所有可能的子字符串,并检查每个子字符串的删除是否使字符串S等于字符串T。如果发现任何字符串为真,则打印“YES” 。否则,打印“NO” 。
时间复杂度: O(N 2 * M)
辅助空间: O(1)
高效的方法:上述方法可以基于以下观察进行优化:
If the substring { S[0], …, S[i] } + { S[N – (M – i)], …, S[N – 1] } is equal to T, only then, string S can be converted to the string T.
请按照以下步骤解决问题:
- 迭代范围[0, M]并检查子串{ S[0], …, S[i] } + { S[N – (M – i)], …, S[N – 1] }是否为是否等于T。如果发现是真的,则打印“YES” 。
- 否则,打印“NO” 。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to check if S can be converted to T
// by removing at most one substring from S
string make_string_S_to_T(string S, string T)
{
// Check if S can be converted to T by
// removing at most one substring from S
bool possible = false;
// Stores length of string T
int M = T.length();
// Stores length of string S
int N = S.length();
// Iterate over the range [0, M - 1]
for (int i = 0; i <= M; i++) {
// Stores Length of the substring
// { S[0], ..., S[i] }
int prefix_length = i;
// Stores Length of the substring
// { S[0], ..., S[i] }
int suffix_length = M - i;
// Stores prefix substring
string prefix
= S.substr(0, prefix_length);
// Stores suffix substring
string suffix
= S.substr(N - suffix_length,
suffix_length);
// Checking if preifx+suffix == T
if (prefix + suffix == T) {
possible = true;
break;
}
}
if (possible)
return "YES";
else
return "NO";
}
// Driver Code
int main()
{
// Given String S and T
string S = "ababcdcd";
string T = "abcd";
// Function call
cout << make_string_S_to_T(S, T);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to check if S can be converted to T
// by removing at most one subString from S
static String make_String_S_to_T(String S, String T)
{
// Check if S can be converted to T by
// removing at most one subString from S
boolean possible = false;
// Stores length of String T
int M = T.length();
// Stores length of String S
int N = S.length();
// Iterate over the range [0, M - 1]
for (int i = 0; i <= M; i++)
{
// Stores Length of the subString
// { S[0], ..., S[i] }
int prefix_length = i;
// Stores Length of the subString
// { S[0], ..., S[i] }
int suffix_length = M - i;
// Stores prefix subString
String prefix
= S.substring(0, prefix_length);
// Stores suffix subString
String suffix
= S.substring(N - suffix_length,
N);
// Checking if preifx+suffix == T
if ((prefix + suffix).equals(T))
{
possible = true;
break;
}
}
if (possible)
return "YES";
else
return "NO";
}
// Driver Code
public static void main(String[] args)
{
// Given String S and T
String S = "ababcdcd";
String T = "abcd";
// Function call
System.out.print(make_String_S_to_T(S, T));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python program for the above approach
# Function to check if S can be converted to T
# by removing at most one substring from S
def make_string_S_to_T(S, T):
# Check if S can be converted to T by
# removing at most one substring from S
possible = False
# Stores length of string T
M = len(T)
# Stores length of string S
N = len(S)
# Iterate over the range [0, M - 1]
for i in range(0,M+1):
# Stores Length of the substring
# S[0], ..., S[i]
prefix_length = i
# Stores Length of the substring
# S[0], ..., S[i]
suffix_length = M - i
# Stores prefix substring
prefix = S[:prefix_length]
# Stores suffix substring
suffix = S[N - suffix_length:N]
# Checking if preifx+suffix == T
if (prefix + suffix == T):
possible = True
break
if (possible):
return "YES"
else:
return "NO"
# Driver Code
# Given String S and T
S = "ababcdcd"
T = "abcd"
# Function call
print(make_string_S_to_T(S, T))
# This code is contributed by shubhamsingh10
C#
// C# program to implement
// the above approach
using System;
public class GFG
{
// Function to check if S can be converted to T
// by removing at most one subString from S
static String make_String_S_to_T(String S, String T)
{
// Check if S can be converted to T by
// removing at most one subString from S
bool possible = false;
// Stores length of String T
int M = T.Length;
// Stores length of String S
int N = S.Length;
// Iterate over the range [0, M - 1]
for (int i = 0; i <= M; i++)
{
// Stores Length of the subString
// { S[0], ..., S[i] }
int prefix_length = i;
// Stores Length of the subString
// { S[0], ..., S[i] }
int suffix_length = M - i;
// Stores prefix subString
String prefix
= S.Substring(0, prefix_length);
// Stores suffix subString
String suffix
= S.Substring(N-suffix_length,
suffix_length);
// Checking if preifx+suffix == T
if ((prefix + suffix).Equals(T))
{
possible = true;
break;
}
}
if (possible)
return "YES";
else
return "NO";
}
// Driver Code
public static void Main(String[] args)
{
// Given String S and T
String S = "ababcdcd";
String T = "abcd";
// Function call
Console.Write(make_String_S_to_T(S, T));
}
}
// This code is contributed by shikhasingrajput
Javascript
YES
时间复杂度: O(M 2 )
辅助空间: O(M)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live