Minimum characters to be replaced to remove the given substring
Last Updated :
28 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
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++) {
if (A[i + j] != B[j])
break ;
}
if (j == m) {
count++;
i += m - 1;
}
}
return count;
}
int main()
{
string str1 = "aaaaaaaa" ;
string str2 = "aaa" ;
cout << replace(str1 , str2);
return 0;
}
|
Java
import java.io.*;
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++)
{
if (i + j >= n)
break ;
else if (A.charAt(i + j) != B.charAt(j))
break ;
}
if (j == m)
{
count++;
i += m - 1 ;
}
}
return count;
}
public static void main(String args[])
{
String str1 = "aaaaaaaa" ;
String str2 = "aaa" ;
System.out.println(replace(str1 , str2));
}
}
|
Python3
def replace(A, B):
n, m = len (A), len (B)
count, i = 0 , 0
while i < n:
j = 0
while j < m:
if i + j > = n or A[i + j] ! = B[j]:
break
j + = 1
if j = = m:
count + = 1
i + = m - 1
i + = 1
return count
if __name__ = = "__main__" :
str1 = "aaaaaaaa"
str2 = "aaa"
print (replace(str1 , str2))
|
C#
using System;
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++)
{
if (i + j >= n)
{
break ;
}
else if (A[i + j] != B[j])
{
break ;
}
}
if (j == m)
{
count++;
i += m - 1;
}
}
return count;
}
public static void Main( string [] args)
{
string str1 = "aaaaaaaa" ;
string str2 = "aaa" ;
Console.WriteLine(replace(str1, str2));
}
}
|
PHP
<?php
function replace( $A , $B )
{
$n = strlen ( $A );
$m = strlen ( $B );
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $m ; $j ++)
{
if ( $i + $j >= $n )
{
break ;
}
else if ( $A [ $i + $j ] != $B [ $j ])
{
break ;
}
}
if ( $j == $m )
{
$count ++;
$i = $i + $m - 1;
}
}
return $count ;
}
$str1 = "aaaaaaaa" ;
$str2 = "aaa" ;
echo (replace( $str1 , $str2 ));
?>
|
Javascript
<script>
function replace(A,B)
{
let n = A.length, m = B.length;
let count = 0, i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (A[i + j] != B[j])
break ;
}
if (j == m) {
count++;
i += m - 1;
}
}
return count;
}
const str1 = "aaaaaaaa" ;
const str2 = "aaa" ;
document.write(replace(str1 , str2));
</script>
|
Time Complexity: O(len1 * len2), where len1 is the length of first string and len2 is the length of second string.
Auxiliary Space: O(1) because it is using constant space for variable
Also, this problem can be solved directly by using Python’s in-built function-string1.count(string2)
Implementation:
C++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "aaaaaaaa" ;
string str2 = "aaa" ;
int answer = 0;
size_t pos = str1.find(str2);
while (pos != string::npos) {
answer++;
pos = str1.find(str2, pos + str2.size());
}
cout << answer << endl;
}
|
Java
public class Main {
public static void main(String[] args) {
String str1 = "aaaaaaaa" ;
String str2 = "aaa" ;
int answer = str1.split(str2, - 1 ).length - 1 ;
System.out.println(answer);
}
}
|
Python3
str1 = "aaaaaaaa"
str2 = "aaa"
answer = str1.count(str2)
print (answer)
|
C#
using System;
class Program {
static void Main( string [] args)
{
string str1 = "aaaaaaaa" ;
string str2 = "aaa" ;
int answer = str1.Split( new [] { str2 },
StringSplitOptions.None)
.Length
- 1;
Console.WriteLine(answer);
}
}
|
Javascript
let str1 = "aaaaaaaa" ;
let str2 = "aaa" ;
let answer = str1.split(str2).length - 1;
console.log(answer);
|
Time Complexity: O(len1 * len2), where len1 is the length of first string and len2 is the length of second string.
Auxiliary Space: O(1) because it is using constant space for variable