Monday, November 18, 2024
Google search engine
HomeData Modelling & AIMinimum characters to be deleted from the end to make given two...

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

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 = “neveropen”, S2 = “neveropenfor” 
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 = "neveropen",
           s2 = "neveropenfor";
 
    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 = "neveropen";
    String s2 = "neveropenfor";
     
    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 = "neveropen";
    s2 = "neveropenfor";
 
    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 = "neveropen";
    String s2 = "neveropenfor";
     
    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 = "neveropen",
s2 = "neveropenfor";
 
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)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments