compare the first string with its only right neighbor
int j = 0 ; while ( j < min ( a [ 0 ] . length ( ) - 1 , a [ 1 ] . length ( ) - 1 ) ) { if ( a [ 0 ] [ j ] == a [ 1 ] [ j ] ) j ++ ; else break ; } int ind = 0 ; res [ ind ++ ] = a [ 0 ] . substr ( 0 , j + 1 ) ;