Given two strings str1 and str2. The task is to find the minimum number of characters to be replaced by $ in string str1 such that str1 does not contain string str2 as any substring.
Examples:
Input: str1 = "intellect", str2 = "tell" Output: 1 4th character of string "str1" can be replaced by $ such that "int$llect" it does not contain "tell" as a substring. Input: str1 = "google", str2 = "apple" Output: 0
Approach is similar to Searching for Patterns | Set 1 (Naive Pattern Searching).
The idea is to find the leftmost occurrence of the string ‘str2’ in the string ‘str1’. If all the characters of ‘str1’ match with ‘str2’, we will replace (or increment our answer with one) the last symbol of occurrence and increment the index of string ‘str1’, such that it checks again for the substring after the replaced character(that is index i will be equal to i+length(b)-1).
Below is the implementation of the above approach:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // function to calculate minimum // characters to replace int replace(string A, string B) { int n = A.length(), m = B.length(); int count = 0, i, j; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { // mismatch occurs if (A[i + j] != B[j]) break; } // if all characters matched, i.e, // there is a substring of 'a' which // is same as string 'b' if (j == m) { count++; // increment i to index m-1 such that // minimum characters are replaced // in 'a' i += m - 1; } } return count; } // Driver Code int main() { string str1 = "aaaaaaaa"; string str2 = "aaa"; cout << replace(str1 , str2); return 0; } |
Java
// Java implementation of // above approach import java.io.*; // function to calculate minimum // characters to replace class GFG { static int replace(String A, String B) { int n = A.length(), m = B.length(); int count = 0, i, j; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { // mismatch occurs if(i + j >= n) break; else if (A.charAt(i + j) != B.charAt(j)) break; } // if all characters matched, i.e, // there is a substring of 'a' which // is same as string 'b' if (j == m) { count++; // increment i to index m-1 such that // minimum characters are replaced // in 'a' i += m - 1; } } return count; } // Driver Code public static void main(String args[]) { String str1 = "aaaaaaaa"; String str2 = "aaa"; System.out.println(replace(str1 , str2)); } } // This code is contributed by Subhadeep |
Python3
# Python3 implementation of the # above approach # Function to calculate minimum # characters to replace def replace(A, B): n, m = len(A), len(B) count, i = 0, 0 while i < n: j = 0 while j < m: # mismatch occurs if i + j >= n or A[i + j] != B[j]: break j += 1 # If all characters matched, i.e, # there is a substring of 'a' which # is same as string 'b' if j == m: count += 1 # increment i to index m-1 such that # minimum characters are replaced # in 'a' i += m - 1 i += 1 return count # Driver Code if __name__ == "__main__": str1 = "aaaaaaaa" str2 = "aaa" print(replace(str1 , str2)) # This code is contributed by Rituraj Jain |
C#
// C# implementation of above approach using System; // function to calculate minimum // characters to replace class GFG { public static int replace(string A, string B) { int n = A.Length, m = B.Length; int count = 0, i, j; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { // mismatch occurs if (i + j >= n) { break; } else if (A[i + j] != B[j]) { break; } } // if all characters matched, i.e, // there is a substring of 'a' // which is same as string 'b' if (j == m) { count++; // increment i to index m-1 // such that minimum characters // are replaced in 'a' i += m - 1; } } return count; } // Driver Code public static void Main(string[] args) { string str1 = "aaaaaaaa"; string str2 = "aaa"; Console.WriteLine(replace(str1, str2)); } } // This code is contributed // by Shrikant13 |
PHP
<?php // PHP implementation of above approach // function to calculate minimum // characters to replace function replace($A, $B) { $n = strlen($A); $m = strlen($B); $count = 0; for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $m; $j++) { // mismatch occurs if ($i + $j >= $n) { break; } else if ($A[$i + $j] != $B[$j]) { break; } } // if all characters matched, i.e, // there is a substring of 'a' // which is same as string 'b' if ($j == $m) { $count++; // increment i to index m-1 // such that minimum characters // are replaced in 'a' $i = $i + $m - 1; } } return $count; } // Driver Code $str1 = "aaaaaaaa"; $str2 = "aaa"; echo (replace($str1, $str2)); // This code is contributed // by Kirti_Mangal ?> |
2
Time Complexity: O(len1 * len2), where len1 is the length of first string and len2 is the length of second string.
Also, this problem can be solved directly by using Python’s in-built function-string1.count(string2)
// Python program to find minimum numbers // of characters to be replaced to // remove the given substring str1 = "aaaaaaaa"str2 = "aaa" # inbuilt function answer = str1.count(str2) print(answer) |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum characters to be replaced to make frequency of all characters same
- Minimum characters to be replaced to make a string concatenation of a K-length palindromic string
- Minimum number of 1's to be replaced in a binary array
- Longest substring with atmost K characters from the given set of characters
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Modify the string such that every character gets replaced with the next character in the keyboard
- Minimum number of steps needed to remove the substring K from given string
- Minimum length of substring whose rotation generates a palindromic substring
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Minimum steps to remove substring 010 from a binary string
- Remove minimum characters from string to split it into three substrings under given constraints
- Minimum length substring with exactly K distinct characters
- Minimum steps to delete a string by deleting substring comprising of same characters
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Count of distinct characters in a substring by given range for Q queries
- Find the longest substring with k unique characters in a given string
- Remove minimum number of characters so that two strings become anagram
- Minimum number of operations to move all uppercase characters before all lower case characters
- Minimum characters that are to be inserted such that no three consecutive characters are same
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.

