Related Articles

# Minimum characters to be deleted from the end to make given two strings equal

• Last Updated : 19 Jun, 2020

Given two strings S1 and S2, the task is to find the minimum number of characters to be deleted from the end of the two strings to make them equal.

Two empty strings will be considered identical.

Examples:

Input: S1 = “abcd”, S2 = “absefr”
Output: 6
Explanation:
Characters {‘d’, ‘c’} are deleted from the end of S1 and characters {‘r’, ‘f’, ‘e’, ‘s’} are deleted from the end of S2 to make the strings equal.

Input: S1 = “geeks”, S2 = “geeksfor”
Output: 3
Explanation:
Characters {‘f’, ‘o’, ‘r’} are deleted from the end of S2.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
Iterate from the beginning of the two strings simultaneously and check if the characters are equal. The first index where the characters in the two strings differ is the index up to which all the characters from the end of both the strings need to be deleted.

Below is the implementation of above approach:

## C++

 `// C++ Program to count minimum``// number of characters to be deleted``// to make the strings equal``#include ``using` `namespace` `std;`` ` `// Function that finds minimum``// character required to be deleted``int` `minDel(string s1, string s2)``{``    ``int` `i = 0;`` ` `    ``// Iterate in the strings``    ``while` `(i < min(s1.length(),``                   ``s2.length())) {`` ` `        ``// Check if the characters are``        ``// not equal``        ``if` `(s1[i] != s2[i]) {`` ` `            ``break``;``        ``}`` ` `        ``i++;``    ``}`` ` `    ``// Return the result``    ``int` `ans = (s1.length() - i)``              ``+ (s2.length() - i);``    ``return` `ans;``}`` ` `// Driver Program``int` `main()``{``    ``string s1 = ``"geeks"``,``           ``s2 = ``"geeksfor"``;`` ` `    ``cout << minDel(s1, s2)``         ``<< endl;``}`

## Java

 `// Java program to count minimum number  ``// of characters to be deleted to make  ``// the strings equal `` ` `class` `GFG{``     ` `// Function that finds minimum ``// character required to be deleted ``static` `int` `minDel(String s1, String s2)``{``    ``int` `i = ``0``;`` ` `    ``// Iterate in the strings ``    ``while` `(i < Math.min(s1.length(), ``                        ``s2.length())) ``    ``{``         ` `        ``// Check if the characters are ``        ``// not equal ``        ``if` `(s1.charAt(i) != s2.charAt(i))``        ``{``            ``break``;``        ``}``        ``i++;``    ``}`` ` `    ``// Return the result ``    ``int` `ans = ((s1.length() - i) + ``               ``(s2.length() - i));``    ``return` `ans;``}`` ` `// Driver code ``public` `static` `void` `main(String[] args)``{``    ``String s1 = ``"geeks"``;``    ``String s2 = ``"geeksfor"``;``     ` `    ``System.out.println(minDel(s1, s2));``}``}`` ` `// This code is contributed by rutvik_56`

## Python3

 `# Python3 program to count minimum number``# of characters to be deleted to make``# the strings equal`` ` `# Function that finds minimum``# character required to be deleted``def` `minDel(s1, s2):``     ` `    ``i ``=` `0``;`` ` `    ``# Iterate in the strings``    ``while` `(i < ``min``(``len``(s1), ``len``(s2))):`` ` `        ``# Check if the characters are``        ``# not equal``        ``if` `(s1[i] !``=` `s2[i]):``            ``break``;`` ` `        ``i ``+``=` `1``;`` ` `    ``# Return the result``    ``ans ``=` `((``len``(s1) ``-` `i) ``+` `(``len``(s2) ``-` `i));``    ``return` `ans;`` ` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``     ` `    ``s1 ``=` `"geeks"``;``    ``s2 ``=` `"geeksfor"``;`` ` `    ``print``(minDel(s1, s2));`` ` `# This code is contributed by Rajput-Ji`

## C#

 `// C# program to count minimum number ``// of characters to be deleted 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 = 0;`` ` `    ``// Iterate in the strings ``    ``while` `(i < Math.Min(s1.Length, ``                        ``s2.Length)) ``    ``{``         ` `        ``// Check if the characters ``        ``// are not equal ``        ``if` `(s1[i] != s2[i])``        ``{``            ``break``;``        ``}``        ``i++;``    ``}`` ` `    ``// Return the result ``    ``int` `ans = ((s1.Length - i) + ``               ``(s2.Length - i));``    ``return` `ans;``}`` ` `// Driver code ``public` `static` `void` `Main(String[] args)``{``    ``String s1 = ``"geeks"``;``    ``String s2 = ``"geeksfor"``;``     ` `    ``Console.WriteLine(minDel(s1, s2));``}``}`` ` `// This code is contributed by Rajput-Ji`
Output:
```3
```

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up