Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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:
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:
Explanation: 
Characters {‘f’, ‘o’, ‘r’} are deleted from the end of S2. 
 

 

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 <bits/stdc++.h>
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


Javascript




<script>
 
// JavaScript Program to count minimum
// number of characters to be deleted
// to make the strings equal
 
// Function that finds minimum
// character required to be deleted
function minDel( s1,  s2)
{
  var 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
  var ans = (s1.length - i)
  + (s2.length - i);
  return ans;
}
 
// Driver Program
var s1 = "geeks",
s2 = "geeksfor";
 
document.write(minDel(s1, s2))
 
// This code is contributed by ukasp.
</script>


Output: 

3

 

Time Complexity: O(min(len(s1), len(s2))) 
Auxiliary Space: O(1)
 



Last Updated : 27 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads