# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. 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
2. 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 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



Output:

2

Time Complexity: O(N * M).

My Personal Notes arrow_drop_up

pawanasipugmailcom

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.