Open In App

Check if a string can be converted to another given string by removal of a substring

Improve
Improve
Like Article
Like
Save
Share
Report

Given two strings S and T of length N and M respectively, the task is to check if the string S can be converted to the string T by removing at most one substring of the string S. If found to be true, then print “YES”. Otherwise, print “NO”.

Example:

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”.

Naive Approach: The simplest approach to solve this problem is to generate all possible substrings of the string S and for each substring, check if its removal makes the string S equal to the string T or not. If found to be true for any string, then print “YES”. Otherwise, print “NO”
Time complexity: O(N2 * M) 
Auxiliary space: O(1)

Efficient Approach: The above approach can be optimized based on the following observations:

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.

Follow the steps below to solve the problem:

  • Iterate over the range [0, M] and check if the substring { S[0], …, S[i] } + { S[N – (M – i)], …, S[N – 1] } is equal to T or not. If found to be true, then print “YES”.
  • Otherwise, print “NO”.

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
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 prefix+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 prefix+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 prefix+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 prefix+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




<script>
 
// Javascript program to implement
// the above approach
 
    // Function to check if S can be converted to T
    // by removing at most one subString from S
     function make_String_S_to_T( S,  T) {
 
        // Check if S can be converted to T by
        // removing at most one subString from S
        var possible = false;
 
        // Stores length of String T
        var M = T.length;
 
        // Stores length of String S
        var N = S.length;
 
        // Iterate over the range [0, M - 1]
        for (i = 0; i <= M; i++) {
 
            // Stores Length of the subString
            // { S[0], ..., S[i] }
            var prefix_length = i;
 
            // Stores Length of the subString
            // { S[0], ..., S[i] }
            var suffix_length = M - i;
 
            // Stores prefix subString
            var prefix = S.substring(0, prefix_length);
 
            // Stores suffix subString
            var suffix = S.substring(N - suffix_length, N);
 
            // Checking if prefix+suffix == T
            if ((prefix + suffix)==(T)) {
                possible = true;
                break;
            }
        }
        if (possible)
            return "YES";
        else
            return "NO";
    }
 
    // Driver Code
     
 
        // Given String S and T
        var S = "ababcdcd";
        var T = "abcd";
 
        // Function call
        document.write(make_String_S_to_T(S, T));
 
// This code is contributed by todaysgaurav
 
</script>


Output: 

YES

 

Time complexity: O(M2)
Auxiliary space: O(M)



Last Updated : 08 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads