Count of strings that become equal to one of the two strings after one removal | Function to return the count of the required strings ; Searching index after longest common prefix ends ; Searching index before longest common suffix ends ; If str1 = str2 ; If only 1 character is different in both the strings ; Checking remaining part of string for equality ; Searching in right of string h ( g to h ) ; Driver code
< ? php function findAnswer ( $ str1 , $ str2 , $ n ) { $ ans = 2 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { if ( $ str1 [ $ i ] != $ str2 [ $ i ] ) { $ l = $ i ; break ; } } for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( $ str1 [ $ i ] != $ str2 [ $ i ] ) { $ r = $ i ; break ; } } if ( $ r < $ l ) return 26 * ( $ n + 1 ) ; else if ( $ l == $ r ) return $ ans ; else { for ( $ i = $ l + 1 ; $ i <= $ r ; $ i ++ ) { if ( $ str1 [ $ i ] != $ str2 [ $ i - 1 ] ) { $ ans -- ; break ; } } for ( $ i = $ l + 1 ; $ i <= $ r ; $ i ++ ) { if ( $ str1 [ $ i - 1 ] != $ str2 [ $ i ] ) { $ ans -- ; break ; } } return $ ans ; } } $ str1 = " toy " ; $ str2 = " try " ; $ n = strlen ( $ str1 ) ; echo findAnswer ( $ str1 , $ str2 , $ n ) ; ? >