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.
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:
# 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]:
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
# Driver Code
if __name__ == “__main__”:
str1 = “aaaaaaaa”
str2 = “aaa”
print(replace(str1 , str2))
# This code is contributed by Rituraj Jain
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)
- Minimum length substring with exactly K distinct characters
- Minimum steps to remove substring 010 from a binary string
- Minimum steps to delete a string by deleting substring comprising of same characters
- Remove minimum number of characters so that two strings become anagram
- Minimum number of 1's to be replaced in a binary array
- Minimum number of operations to move all uppercase characters before all lower case characters
- Maximum length substring having all same characters after k changes
- Length of the longest substring with consecutive characters
- Searching characters and substring in a String in Java
- Longest substring of only 4's from the first N characters of the infinite string
- Print Longest substring without repeating characters
- Length of the longest substring without repeating characters
- Find the longest substring with k unique characters in a given string
- Remove characters that appear more than k times
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.