Minimum changes required to make first string substring of second string
Last Updated :
28 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int minimumChar(string S1, string S2)
{
int n = S1.size(), m = S2.size();
int ans = INT_MAX;
for ( int i = 0; i < m - n + 1; i++) {
int minRemovedChar = 0;
for ( int j = 0; j < n; j++) {
if (S1[j] != S2[i + j]) {
minRemovedChar++;
}
}
ans = min(minRemovedChar, ans);
}
return ans;
}
int main()
{
string S1 = "abc" ;
string S2 = "paxzk" ;
cout << minimumChar(S1, S2);
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
#include <limits.h>
int min( int a, int b)
{
int min = a;
if (min > b)
min = b;
return min;
}
int minimumChar( char S1[], char S2[])
{
int n = strlen (S1), m = strlen (S2);
int ans = INT_MAX;
for ( int i = 0; i < m - n + 1; i++) {
int minRemovedChar = 0;
for ( int j = 0; j < n; j++) {
if (S1[j] != S2[i + j]) {
minRemovedChar++;
}
}
ans = min(minRemovedChar, ans);
}
return ans;
}
int main()
{
char S1[] = "abc" ;
char S2[] = "paxzk" ;
printf ( "%d" ,minimumChar(S1, S2));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int minimumChar(String S1,
String S2)
{
int n = S1.length();
int m = S2.length();
int ans = Integer.MAX_VALUE ;
for ( int i = 0 ; i < m - n + 1 ; i++)
{
int minRemovedChar = 0 ;
for ( int j = 0 ; j < n; j++)
{
if (S1.charAt(j) != S2.charAt(i + j))
{
minRemovedChar++;
}
}
ans = Math.min(minRemovedChar, ans);
}
return ans;
}
public static void main (String[] args)
{
String S1 = "abc" ;
String S2 = "paxzk" ;
System.out.println(minimumChar(S1, S2));
}
}
|
Python3
import sys
def minimumChar(S1, S2):
n, m = len (S1), len (S2)
ans = sys.maxsize
for i in range (m - n + 1 ):
minRemovedChar = 0
for j in range (n):
if (S1[j] ! = S2[i + j]):
minRemovedChar + = 1
ans = min (minRemovedChar, ans)
return ans
if __name__ = = '__main__' :
S1 = "abc"
S2 = "paxzk"
print (minimumChar(S1, S2))
|
C#
using System;
class GFG
{
static int minimumChar(String S1,
String S2)
{
int n = S1.Length;
int m = S2.Length;
int ans = Int32.MaxValue ;
for ( int i = 0; i < m - n + 1; i++)
{
int minRemovedChar = 0;
for ( int j = 0; j < n; j++)
{
if (S1[j] != S2[i + j])
{
minRemovedChar++;
}
}
ans = Math.Min(minRemovedChar, ans);
}
return ans;
}
public static void Main()
{
String S1 = "abc" ;
String S2 = "paxzk" ;
Console.WriteLine(minimumChar(S1, S2));
}
}
|
PHP
<?php
function minimumChar( $S1 , $S2 )
{
$n = strlen ( $S1 );
$m = strlen ( $S2 );
$ans = PHP_INT_MAX;
for ( $i = 0; $i < $m - $n + 1; $i ++)
{
$minRemovedChar = 0;
for ( $j = 0; $j < $n ; $j ++)
{
if ( $S1 [ $j ] != $S2 [ $i + $j ])
{
$minRemovedChar ++;
}
}
$ans = min( $minRemovedChar , $ans );
}
return $ans ;
}
$S1 = "abc" ;
$S2 = "paxzk" ;
echo minimumChar( $S1 , $S2 );
?>
|
Javascript
<script>
function minimumChar(S1, S2)
{
let n = S1.length;
let m = S2.length;
let ans = Number.MAX_VALUE;
for (let i = 0; i < m - n + 1; i++)
{
let minRemovedChar = 0;
for (let j = 0; j < n; j++)
{
if (S1[j] != S2[i + j])
{
minRemovedChar++;
}
}
ans = Math.min(minRemovedChar, ans);
}
return ans;
}
let S1 = "abc" ;
let S2 = "paxzk" ;
document.write(minimumChar(S1, S2));
</script>
|
Time Complexity: O(N * M)
Auxiliary Space: O(1) it is using constant space for variables
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...