给定两个字符串S1和S2(S1的大小<= S2的大小)。任务是找到字符串S2中要替换的最少字符数,以使字符串S1是S2的子字符串。
例子:
Input : S1 = cdef, S2 = abbdef
Output : 1
Input : S1 = gfg, S2 = fgg
Output : 2
方法:
- 遍历字符串S2
- 从S2中的每个索引中,检查S1长度的子字符串中不匹配的字符数
- 在ans中存储和更新上一个和当前不匹配的最小值
- 返回ans。
下面是上述方法的实现:
C++
// CPP program to find the minimum number of
// characters to be replaced in string S2, such
// that S1 is a substring of S2
#include
using namespace std;
// Function to find the minimum number of
// characters to be replaced in string S2, such
// that S1 is a substring of S2
int minimumChar(string S1, string S2)
{
// Get the sizes of both strings
int n = S1.size(), m = S2.size();
int ans = INT_MAX;
// Traverse the string S2
for (int i = 0; i < m - n + 1; i++) {
int minRemovedChar = 0;
// From every index in S2, check the number of
// mis-matching characters in substring of
// length of S1
for (int j = 0; j < n; j++) {
if (S1[j] != S2[i + j]) {
minRemovedChar++;
}
}
// Take minimum of prev and current mis-match
ans = min(minRemovedChar, ans);
}
// return answer
return ans;
}
// Driver Code
int main()
{
string S1 = "abc";
string S2 = "paxzk";
cout << minimumChar(S1, S2);
return 0;
}
Java
// Java program to find the minimum
// number of characters to be
// replaced in string S2, such
// that S1 is a substring of S2
import java.io.*;
class GFG
{
// Function to find the minimum
// number of characters to be
// replaced in string S2, such
// that S1 is a substring of S2
static int minimumChar(String S1,
String S2)
{
// Get the sizes of both strings
int n = S1.length();
int m = S2.length();
int ans = Integer.MAX_VALUE ;
// Traverse the string S2
for (int i = 0; i < m - n + 1; i++)
{
int minRemovedChar = 0;
// From every index in S2, check
// the number of mis-matching
// characters in substring of
// length of S1
for (int j = 0; j < n; j++)
{
if (S1.charAt(j) != S2.charAt(i + j))
{
minRemovedChar++;
}
}
// Take minimum of prev and
// current mis-match
ans = Math.min(minRemovedChar, ans);
}
// return answer
return ans;
}
// Driver Code
public static void main (String[] args)
{
String S1 = "abc";
String S2 = "paxzk";
System.out.println(minimumChar(S1, S2));
}
}
// This code is contributed by Shashank
Python3
# Python3 program to find the minimum
# number of characters to be replaced
# in string S2, such that S1 is a
# substring of S2
import sys
# Function to find the minimum number of
# characters to be replaced in string S2,
# such that S1 is a substring of S2
def minimumChar(S1, S2):
# Get the sizes of both strings
n, m = len(S1), len(S2)
ans = sys.maxsize
# Traverse the string S2
for i in range(m - n + 1):
minRemovedChar = 0
# From every index in S2, check the
# number of mis-matching characters
# in substring of length of S1
for j in range(n):
if (S1[j] != S2[i + j]):
minRemovedChar += 1
# Take minimum of prev and
# current mis-match
ans = min(minRemovedChar, ans)
# return answer
return ans
# Driver Code
if __name__ == '__main__':
S1 = "abc"
S2 = "paxzk"
print(minimumChar(S1, S2))
# This code is contributed
# by PrinciRaj1992
C#
// C# program to find the minimum
// number of characters to be
// replaced in string S2, such
// that S1 is a substring of S2
using System;
class GFG
{
// Function to find the minimum
// number of characters to be
// replaced in string S2, such
// that S1 is a substring of S2
static int minimumChar(String S1,
String S2)
{
// Get the sizes of both strings
int n = S1.Length;
int m = S2.Length;
int ans = Int32.MaxValue ;
// Traverse the string S2
for (int i = 0; i < m - n + 1; i++)
{
int minRemovedChar = 0;
// From every index in S2, check
// the number of mis-matching
// characters in substring of
// length of S1
for (int j = 0; j < n; j++)
{
if (S1[j] != S2[i + j])
{
minRemovedChar++;
}
}
// Take minimum of prev and
// current mis-match
ans = Math.Min(minRemovedChar, ans);
}
// return answer
return ans;
}
// Driver Code
public static void Main()
{
String S1 = "abc";
String S2 = "paxzk";
Console.WriteLine(minimumChar(S1, S2));
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
PHP
输出:
2
时间复杂度:O(N * M)。