Check if a string can be converted to another given string by removal of a substring | C ++ program to implement the above approach ; Function to check if S can be converted to T by removing at most one substring from S ; Check if S can be converted to T by removing at most one substring from S ; Stores length of string T ; Stores length of string S ; Iterate over the range [ 0 , M - 1 ] ; Stores Length of the substring { S [ 0 ] , ... , S [ i ] } ; Stores Length of the substring { S [ 0 ] , ... , S [ i ] } ; Stores prefix substring ; Stores suffix substring ; Checking if prefix + suffix == T ; Driver Code ; Given String S and T ; Function call
#include <bits/stdc++.h> NEW_LINE 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 ; }