Converting one string to other using append and delete last operations | java Program to convert str1 to str2 in exactly k operations ; Returns true if it is possible to convert str1 to str2 using k operations . ; Case A ( i ) ; finding common length of both string ; Case A ( ii ) - ; Case B ; Driver program
import java . io . * ; class GFG { static boolean isConvertible ( String str1 , String str2 , int k ) { if ( ( str1 . length ( ) + str2 . length ( ) ) < k ) return true ; int commonLength = 0 ; for ( int i = 0 ; i < Math . min ( str1 . length ( ) , str2 . length ( ) ) ; i ++ ) { if ( str1 == str2 ) commonLength ++ ; else break ; } if ( ( k - str1 . length ( ) - str2 . length ( ) + 2 * commonLength ) % 2 == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { String str1 = " geek " ; String str2 = " geek " ; int k = 7 ; if ( isConvertible ( str1 , str2 , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; str1 = " geeks " ; str2 = " geek " ; k = 5 ; if ( isConvertible ( str1 , str2 , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }