Given two strings **S** and **T**, the task is to find the minimum number of characters to be deleted from the beginning of these strings to make the two strings identical.

Two empty strings will always be an identical strings.

**Examples:**

Input:S = “geeksforgeeks” T = “peeks”

Output:10

Explanation:

Substring “geeksforg” from S and “p” from T are deleted to make both the strings equals to “eeks”

Input:S = “geeksforgeeks” T = “code”

Output:17

Explanation:

Both the strings had to be deleted completely.

**Approach:**

Traverse both the strings simultaneously from the end and compare the characters of the two strings. The first indices **i** (index of S1) and **j** (index of S2) where the characters of the two strings differ is the length up to which the characters of S1 and S2 need to be deleted. Hence, the final value of **i + j **is the required answer.

Below is the implementation of the above approach:

## C++

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

*chevron_right*

*filter_none*

## Java

`// Java Program to count minimum ` `// number of characters to be deleted ` `// from the beginning of the two strings ` `// to make the strings equal ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `// Function that finds minimum ` `// character required to be deleted ` `static` `int` `minDel(String s1, String s2) ` `{ ` ` ` `int` `i = s1.length(); ` ` ` `int` `j = s2.length(); ` ` ` ` ` `// Iterate in the strings ` ` ` `while` `(i > ` `0` `&& j > ` `0` `) ` ` ` `{ ` ` ` ` ` `// Check if the characters are ` ` ` `// not equal ` ` ` `if` `(s1.charAt(i - ` `1` `) != s2.charAt(j - ` `1` `)) ` ` ` `{ ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `i--; ` ` ` `j--; ` ` ` `} ` ` ` ` ` `// Return the result ` ` ` `return` `i + j; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `String s1 = ` `"geeksforgeeks"` `, ` ` ` `s2 = ` `"peeks"` `; ` ` ` ` ` `System.out.print(minDel(s1, s2)); ` `} ` `} ` ` ` `// This code is contributed by Nidhi_biet ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 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 ` `def` `minDel(s1, s2): ` ` ` ` ` `i ` `=` `len` `(s1) ` ` ` `j ` `=` `len` `(s2) ` ` ` ` ` `# Iterate in the strings ` ` ` `while` `(i > ` `0` `and` `j > ` `0` `): ` ` ` ` ` `# Check if the characters are ` ` ` `# not equal ` ` ` `if` `(s1[i ` `-` `1` `] !` `=` `s2[j ` `-` `1` `]): ` ` ` `break` ` ` ` ` `i ` `-` `=` `1` ` ` `j ` `-` `=` `1` ` ` ` ` `# Return the result ` ` ` `return` `i ` `+` `j ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `s1 ` `=` `"geeksforgeeks"` ` ` `s2 ` `=` `"peeks"` ` ` ` ` `print` `(minDel(s1, s2)) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to count minimum ` `// number of characters to be deleted ` `// from the beginning of the two strings ` `// to make the strings equal ` `using` `System; ` `class` `GFG{ ` ` ` `// Function that finds minimum ` `// character required to be deleted ` `static` `int` `minDel(` `string` `s1, ` `string` `s2) ` `{ ` ` ` `int` `i = s1.Length; ` ` ` `int` `j = s2.Length; ` ` ` ` ` `// Iterate in the strings ` ` ` `while` `(i > 0 && j > 0) ` ` ` `{ ` ` ` ` ` `// Check if the characters are ` ` ` `// not equal ` ` ` `if` `(s1[i - 1] != s2[j - 1]) ` ` ` `{ ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `i--; ` ` ` `j--; ` ` ` `} ` ` ` ` ` `// Return the result ` ` ` `return` `i + j; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `string` `s1 = ` `"geeksforgeeks"` `, ` ` ` `s2 = ` `"peeks"` `; ` ` ` ` ` `Console.Write(minDel(s1, s2)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

10

**Time Complexity:** O( min(len(S1), len(S2)) )

**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum characters to be deleted from the end to make given two strings equal
- Number of character corrections in the given strings to make them equal
- Length of longest substring to be deleted to make a string equal to another string
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Count columns to be deleted to make each row sorted
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Minimum number of given operations required to make two strings equal
- Find the minimum number of preprocess moves required to make two strings equal
- Minimum swaps to make two strings equal by swapping only with third string
- Minimum number of operations required to make two strings equal
- Minimum characters to be replaced to make frequency of all characters same
- Minimum operations to make frequency of all characters equal K
- Minimum move to end operations to make all strings equal
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Count of characters in str1 such that after deleting anyone of them str1 becomes str2
- Count of strings that become equal to one of the two strings after one removal
- Remove minimum number of characters so that two strings become anagram
- Sort an Array of Strings according to the number of Vowels in them
- Sort the strings based on the numbers of matchsticks required to represent them
- Minimum Cost To Make Two Strings Identical

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.