Check if a string can be converted to another given string by removal of a substring | Java 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
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 ) ) ; } }