Check if a string can be converted to another given string by removal of a substring
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++
#include <bits/stdc++.h>
using namespace std;
string make_string_S_to_T(string S, string T)
{
bool possible = false ;
int M = T.length();
int N = S.length();
for ( int i = 0; i <= M; i++) {
int prefix_length = i;
int suffix_length = M - i;
string prefix
= S.substr(0, prefix_length);
string suffix
= S.substr(N - suffix_length,
suffix_length);
if (prefix + suffix == T) {
possible = true ;
break ;
}
}
if (possible)
return "YES" ;
else
return "NO" ;
}
int main()
{
string S = "ababcdcd" ;
string T = "abcd" ;
cout << make_string_S_to_T(S, T);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String make_String_S_to_T(String S, String T)
{
boolean possible = false ;
int M = T.length();
int N = S.length();
for ( int i = 0 ; i <= M; i++)
{
int prefix_length = i;
int suffix_length = M - i;
String prefix
= S.substring( 0 , prefix_length);
String suffix
= S.substring(N - suffix_length,
N);
if ((prefix + suffix).equals(T))
{
possible = true ;
break ;
}
}
if (possible)
return "YES" ;
else
return "NO" ;
}
public static void main(String[] args)
{
String S = "ababcdcd" ;
String T = "abcd" ;
System.out.print(make_String_S_to_T(S, T));
}
}
|
Python3
def make_string_S_to_T(S, T):
possible = False
M = len (T)
N = len (S)
for i in range ( 0 ,M + 1 ):
prefix_length = i
suffix_length = M - i
prefix = S[:prefix_length]
suffix = S[N - suffix_length:N]
if (prefix + suffix = = T):
possible = True
break
if (possible):
return "YES"
else :
return "NO"
S = "ababcdcd"
T = "abcd"
print (make_string_S_to_T(S, T))
|
C#
using System;
public class GFG
{
static String make_String_S_to_T(String S, String T)
{
bool possible = false ;
int M = T.Length;
int N = S.Length;
for ( int i = 0; i <= M; i++)
{
int prefix_length = i;
int suffix_length = M - i;
String prefix
= S.Substring(0, prefix_length);
String suffix
= S.Substring(N-suffix_length,
suffix_length);
if ((prefix + suffix).Equals(T))
{
possible = true ;
break ;
}
}
if (possible)
return "YES" ;
else
return "NO" ;
}
public static void Main(String[] args)
{
String S = "ababcdcd" ;
String T = "abcd" ;
Console.Write(make_String_S_to_T(S, T));
}
}
|
Javascript
<script>
function make_String_S_to_T( S, T) {
var possible = false ;
var M = T.length;
var N = S.length;
for (i = 0; i <= M; i++) {
var prefix_length = i;
var suffix_length = M - i;
var prefix = S.substring(0, prefix_length);
var suffix = S.substring(N - suffix_length, N);
if ((prefix + suffix)==(T)) {
possible = true ;
break ;
}
}
if (possible)
return "YES" ;
else
return "NO" ;
}
var S = "ababcdcd" ;
var T = "abcd" ;
document.write(make_String_S_to_T(S, T));
</script>
|
Time complexity: O(M2)
Auxiliary space: O(M)
Last Updated :
08 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...