📌  相关文章
📜  从末尾删除的最少字符以使给定的两个字符串相等

📅  最后修改于: 2021-09-03 03:43:24             🧑  作者: Mango

给定两个字符串S1S2 ,任务是找到从两个字符串的末尾删除的最少字符数,以使它们相等。

例子:

方法:
同时从两个字符串的开头迭代并检查字符是否相等。其中两个字符串的字符不同的第一个索引是索引到其从两个字符串的末尾所有字符需要被删除。

下面是上述方法的实现:

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


输出:
3

时间复杂度: O(min(len(s1), len(s2)))
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live