Check whether str1 can be converted to str2 with the given operations | Java implementation of the approach ; Function that returns true if str1 can be converted to str2 with the given operations ; Traverse from left to right ; If the two characters do not match ; If possible to combine ; If not possible to combine ; If the two characters match ; If possible to convert one string to another ; Driver code
class GFG { static boolean canConvert ( String str1 , String str2 ) { int i = 0 , j = 0 ; while ( i < str1 . length ( ) && j < str2 . length ( ) ) { if ( str1 . charAt ( i ) != str2 . charAt ( j ) ) { if ( str1 . charAt ( i ) == '0' && str2 . charAt ( j ) == '1' && i + 1 < str1 . length ( ) && str1 . charAt ( i + 1 ) == '0' ) { i += 2 ; j ++ ; } else { return false ; } } else { i ++ ; j ++ ; } } if ( i == str1 . length ( ) && j == str2 . length ( ) ) return true ; return false ; } public static void main ( String [ ] args ) { String str1 = "00100" , str2 = "111" ; if ( canConvert ( str1 , str2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }