Minimum changes required to make first string substring of second string
Given two strings S1 and S2 ( size of S1 <= Size of S2 ). The task is to find the minimum number of characters to be replaced in the string S2, such that the string S1 is a substring of S2.
Examples:
Input : S1 = cdef, S2 = abbdef Output : 1 Input : S1 = gfg, S2 = fgg Output : 2
Approach:
- Traverse the string S2
- From every index in S2, check the number of mis-matching characters in substring of length of S1
- Store and update the minimum of prev and current mis-match in ans
- Return ans.
Below is the implementation of the above approach:
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 <bits/stdc++.h> 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
<?php // PHP program to find the minimum // number of characters to be replaced // in string S2, such that S1 is a // substring of S2 // Function to find the minimum number // of characters to be replaced in // string S2, such that S1 is a // substring of S2 function minimumChar($S1, $S2) { // Get the sizes of both strings $n = strlen($S1); $m = strlen($S2); $ans = PHP_INT_MAX; // Traverse the string S2 for ($i = 0; $i < $m - $n + 1; $i++) { $minRemovedChar = 0; // From every index in S2, check // the number of mis-matching // characters in substring of // length of S1 for ($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 $S1 = "abc"; $S2 = "paxzk"; echo minimumChar($S1, $S2); // This code is contributed by ajit. ?> |
2
Time Complexity: O(N * M).
Recommended Posts:
- Minimum swaps required to make a binary string alternating
- Minimum number of given operations required to convert a string to another string
- Operations required to make the string empty
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum rotations required to get the same String | Set-2
- Minimum rotations required to get the same string
- Minimum steps to remove substring 010 from a binary string
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Minimum changes required such that the string satisfies the given condition
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum swaps required to convert one binary string to another
- Minimum steps to delete a string by deleting substring comprising of same characters
- Minimum changes to a string to make all substrings distinct
- Minimum cost to make a string free of a subsequence
- Minimum number of deletions to make a string palindrome | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



