给定两个字符串S和T ,任务是找到从这些字符串的开头删除的最少字符数,以使这两个字符串相同。
Two empty strings will always be an identical strings.
例子:
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.
方法:
从结束的同时遍历两个字符串,并比较两个字符串的字符。第一指数i(S1的指数)和j(S2的指数),其中两个字符串的字符不同的是长度可达其中要被删除S1和S2需要的字符。因此, i + j的最终值是所需的答案。
下面是上述方法的实现:
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
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;
}
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
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
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
Javascript
输出:
10
时间复杂度: O( min(len(S1), len(S2)) )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live