Minimum characters to be deleted from the beginning of two strings to make them equal | C ++ Program to count minimum number of characters to be deleted from the beginning of the two strings to make the strings equal ; Function that finds minimum character required to be deleted ; Iterate in the strings ; Check if the characters are not equal ; Return the result ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDel ( string s1 , string s2 ) { int i = s1 . length ( ) ; int j = s2 . length ( ) ; while ( i > 0 && j > 0 ) { if ( s1 [ i - 1 ] != s2 [ j - 1 ] ) { break ; } i -- ; j -- ; } return i + j ; } int main ( ) { string s1 = " geeksforgeeks " , s2 = " peeks " ; cout << minDel ( s1 , s2 ) << endl ; }