Count of characters in str1 such that after deleting anyone of them str1 becomes str2 | Below is C ++ implementation of the approach ; Function to return the count of required indices ; Solution doesn 't exist ; Find the length of the longest common prefix of strings ; Find the length of the longest common suffix of strings ; If solution does not exist ; Return the count of indices ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Find_Index ( string str1 , string str2 ) { int n = str1 . size ( ) ; int m = str2 . size ( ) ; int l = 0 ; int r = 0 ; if ( n != m + 1 ) { return -1 ; } for ( int i = 0 ; i < m ; i ++ ) { if ( str1 [ i ] == str2 [ i ] ) { l += 1 ; } else { break ; } } int i = n - 1 ; int j = m - 1 ; while ( i >= 0 && j >= 0 && str1 [ i ] == str2 [ j ] ) { r += 1 ; i -= 1 ; j -= 1 ; } if ( l + r < m ) { return -1 ; } else { i = max ( n - r , 1 ) ; j = min ( l + 1 , n ) ; return ( j - i + 1 ) ; } } int main ( ) { string str1 = " aaa " , str2 = " aa " ; cout << Find_Index ( str1 , str2 ) ; return 0 ; }