📌  相关文章
📜  检查是否可以通过删除子字符串将字符串转换为另一个给定的字符串

📅  最后修改于: 2021-04-24 14:20:06             🧑  作者: Mango

给定两个字符串S和分别的长度NM T,该任务是检查字符串S可以通过在字符串S的至多一个子除去而转化为字符串T。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

天真的方法:解决此问题的最简单方法是生成字符串S的所有可能的子字符串,并针对每个子字符串,检查其删除是否使字符串S等于字符串T。如果发现对于任何字符串都是正确的,则打印“ YES” 。否则,打印“否”
时间复杂度: O(N 2 * M)
辅助空间: O(1)

高效的方法:可以基于以下观察来优化上述方法:

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

  • 迭代范围[0,M],并检查子字符串{S [0],…,S [i]} + {S [N –(M – i)],…,S [N – 1]}等于或不等于T。如果发现是真的,则打印“是”
  • 否则,打印“否”

下面是上述方法的实现:

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


输出:
YES

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