Remove all occurrences of a word from a given string using Z-algorithm
Last Updated :
07 Nov, 2022
Given two strings str of length N and word of length M, the task is to remove all the occurrences of the string word from the string str.
Examples:
Input: str = “asmGeeksasmasmForasmGeeks”, word = “asm”
Output: GeeksForGeeks
Explanation:
Removing “asm” from the string, str modifies str to GeeksForGeeks
Input: str = “Z-kmalgorithmkmiskmkmkmhelpfulkminkmsearching”, word = “km”
Output: Z-algorithmishelpfulinsearching
Explanation:
Removing “km” from the string, str modifies str to “Z-algorithmishelpfulinsearching”.
Naive Approach: The simplest approach to solve this problem is to iterate over the characters of the string str. For every index, check if a substring can be found whose starting index is equal to the current index and the substring is equal to the string, word. If found to be true, then remove the substring. Finally, print the string.
Time Complexity: O(N2)
Auxiliary Space: O(1)
STL-based Approach: Remove all the occurrence of the string word from the string str using replace() method.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized using Z-algorithm. Follow the steps below to solve the problem:
- Initialize a string, say res, to store the string by removing the words from the given string str.
- Initialize an array, say Z[], to store the Z-value of the string.
- Find all occurrences of the string word in the given string str using Z-algorithm.
- Finally, traverse the array Z[] and check if z[i + length(word) + 1] is equal to length(word) or not. If found to be true, then update i += length(word) – 1.
- Otherwise, append current character into the string res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void getZarr(string str, int Z[])
{
int n = str.length();
int k;
int L = 0;
int R = 0;
for ( int i = 1; i < n; ++i) {
if (i > R) {
L = R = i;
while (R < n && str[R - L] == str[R]) {
R++;
}
Z[i] = R - L;
R--;
}
else {
k = i - L;
if (Z[k] < R - i + 1) {
Z[i] = Z[k];
}
else {
L = i;
while (R < n && str[R - L] == str[R]) {
R++;
}
Z[i] = R - L;
R--;
}
}
}
}
string goodStr(string str, string word)
{
string concat = word + "$" + str;
int l = concat.length();
int Z[l];
getZarr(concat, Z);
string res;
int pSize = word.size();
for ( int i = 0; i < l; ++i) {
if (i + pSize < l - 1 && Z[i + pSize + 1] == pSize) {
i += pSize - 1;
}
else if (i < str.length()) {
res += str[i];
}
}
return res;
}
int main()
{
string str
= "Z-kmalgorithmkmiskmkmkmhelpfulkminkmsearching" ;
string word = "km" ;
cout << goodStr(str, word);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void getZarr(String str, int Z[])
{
int n = str.length();
int k;
int L = 0 ;
int R = 0 ;
for ( int i = 1 ; i < n; ++i)
{
if (i > R)
{
L = R = i;
while (R < n && str.charAt(R - L) ==
str.charAt(R))
{
R++;
}
Z[i] = R - L;
R--;
}
else
{
k = i - L;
if (Z[k] < R - i + 1 )
{
Z[i] = Z[k];
}
else
{
L = i;
while (R < n && str.charAt(R - L) ==
str.charAt(R))
{
R++;
}
Z[i] = R - L;
R--;
}
}
}
}
static String goodStr(String str, String word)
{
String concat = word + "$" + str;
int l = concat.length();
int []Z = new int [l];
getZarr(concat, Z);
String res= "" ;
int pSize = word.length();
for ( int i = 0 ; i < l; ++i) {
if (i + pSize < l - 1 && Z[i + pSize + 1 ] == pSize) {
i += pSize - 1 ;
}
else if (i < str.length()) {
res += str.charAt(i);
}
}
return res;
}
public static void main(String[] args)
{
String str
= "Z-kmalgorithmkmiskmkmkmhelpfulkminkmsearching" ;
String word = "km" ;
System.out.print(goodStr(str, word));
}
}
|
Python3
def getZarr(st, Z):
n = len (st)
k = 0
L = 0
R = 0
for i in range ( 1 , n):
if (i > R):
L = R = i
while (R < n and st[R - L] = = st[R]):
R + = 1
Z[i] = R - L
R - = 1
else :
k = i - L
if (Z[k] < R - i + 1 ):
Z[i] = Z[k]
else :
L = i
while (R < n and st[R - L] = = st[R]):
R + = 1
Z[i] = R - L
R - = 1
def goodStr(st, word):
concat = word + "$" + st
l = len (concat)
Z = [ 0 ] * l
getZarr(concat, Z)
res = ""
pSize = len (word)
for i in range (l):
if (i + pSize < l - 1 and Z[i + pSize + 1 ] = = pSize):
i + = pSize - 1
elif (i < len (st)):
res + = st[i]
return res
if __name__ = = "__main__" :
st = "Z-kmalgorithmkmiskmkmkmhelpfulkminkmsearching"
word = "km"
print (goodStr(st, word))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void getZarr( string str, int [] Z)
{
int n = str.Length;
int k;
int L = 0;
int R = 0;
for ( int i = 1; i < n; ++i)
{
if (i > R)
{
L = R = i;
while (R < n && str[R - L] ==
str[R])
{
R++;
}
Z[i] = R - L;
R--;
}
else
{
k = i - L;
if (Z[k] < R - i + 1)
{
Z[i] = Z[k];
}
else
{
L = i;
while (R < n && str[R - L] ==
str[R])
{
R++;
}
Z[i] = R - L;
R--;
}
}
}
}
static string goodStr( string str, string word)
{
string concat = word + "$" + str;
int l = concat.Length;
int []Z = new int [l];
getZarr(concat, Z);
string res= "" ;
int pSize = word.Length;
for ( int i = 0; i < l; ++i) {
if (i + pSize < l - 1 && Z[i + pSize + 1] == pSize) {
i += pSize - 1;
}
else if (i < str.Length) {
res += str[i];
}
}
return res;
}
static public void Main()
{
string str
= "Z-kmalgorithmkmiskmkmkmhelpfulkminkmsearching" ;
string word = "km" ;
Console.WriteLine(goodStr(str, word));
}
}
|
Javascript
<script>
function getZarr(str,Z)
{
let n = str.length;
let k;
let L = 0;
let R = 0;
for (let i = 1; i < n; ++i)
{
if (i > R)
{
L = R = i;
while (R < n && str[R - L] ==
str[R])
{
R++;
}
Z[i] = R - L;
R--;
}
else
{
k = i - L;
if (Z[k] < R - i + 1)
{
Z[i] = Z[k];
}
else
{
L = i;
while (R < n && str[R - L] ==
str[R])
{
R++;
}
Z[i] = R - L;
R--;
}
}
}
}
function goodStr(str,word)
{
let concat = word + "$" + str;
let l = concat.length;
let Z = new Array(l);
getZarr(concat, Z);
let res= "" ;
let pSize = word.length;
for (let i = 0; i < l; ++i) {
if (i + pSize < l - 1 && Z[i + pSize + 1] == pSize) {
i += pSize - 1;
}
else if (i < str.length) {
res += str[i];
}
}
return res;
}
let str = "Z-kmalgorithmkmiskmkmkmhelpfulkminkmsearching" ;
let word = "km" ;
document.write(goodStr(str, word));
</script>
|
C++
#include <bits/stdc++.h>
using namespace std;
void getZarr(string str, int Z[])
{
int n = str.length();
int k;
int L = 0;
int R = 0;
for ( int i = 1; i < n; ++i) {
if (i > R) {
L = R = i;
while (R < n && str[R - L] == str[R]) {
R++;
}
Z[i] = R - L;
R--;
}
else {
k = i - L;
if (Z[k] < R - i + 1) {
Z[i] = Z[k];
}
else {
L = i;
while (R < n && str[R - L] == str[R]) {
R++;
}
Z[i] = R - L;
R--;
}
}
}
}
string goodStr(string str, string word)
{
string concat = word + "$" + str;
int l = concat.length();
int Z[l];
getZarr(concat, Z);
string res;
int pSize = word.size();
for ( int i = 0; i < l; ++i) {
if (i + pSize < l - 1 && Z[i + pSize + 1] == pSize) {
i += pSize - 1;
}
else if (i < str.length()) {
res += str[i];
}
}
return res;
}
int main()
{
string str
= "Z-kmalgorithmkmiskmkmkmhelpfulkminkmsearching" ;
string word = "km" ;
cout << goodStr(str, word);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void getZarr(String str, int Z[])
{
int n = str.length();
int k;
int L = 0 ;
int R = 0 ;
for ( int i = 1 ; i < n; ++i)
{
if (i > R)
{
L = R = i;
while (R < n && str.charAt(R - L) ==
str.charAt(R))
{
R++;
}
Z[i] = R - L;
R--;
}
else
{
k = i - L;
if (Z[k] < R - i + 1 )
{
Z[i] = Z[k];
}
else
{
L = i;
while (R < n && str.charAt(R - L) ==
str.charAt(R))
{
R++;
}
Z[i] = R - L;
R--;
}
}
}
}
static String goodStr(String str, String word)
{
String concat = word + "$" + str;
int l = concat.length();
int []Z = new int [l];
getZarr(concat, Z);
String res= "" ;
int pSize = word.length();
for ( int i = 0 ; i < l; ++i) {
if (i + pSize < l - 1 && Z[i + pSize + 1 ] == pSize) {
i += pSize - 1 ;
}
else if (i < str.length()) {
res += str.charAt(i);
}
}
return res;
}
public static void main(String[] args)
{
String str
= "Z-kmalgorithmkmiskmkmkmhelpfulkminkmsearching" ;
String word = "km" ;
System.out.print(goodStr(str, word));
}
}
|
Python3
def getZarr(st, Z):
n = len (st)
k = 0
L = 0
R = 0
for i in range ( 1 , n):
if (i > R):
L = R = i
while (R < n and st[R - L] = = st[R]):
R + = 1
Z[i] = R - L
R - = 1
else :
k = i - L
if (Z[k] < R - i + 1 ):
Z[i] = Z[k]
else :
L = i
while (R < n and st[R - L] = = st[R]):
R + = 1
Z[i] = R - L
R - = 1
def goodStr(st, word):
concat = word + "$" + st
l = len (concat)
Z = [ 0 ] * l
getZarr(concat, Z)
res = ""
pSize = len (word)
for i in range (l):
if (i + pSize < l - 1 and Z[i + pSize + 1 ] = = pSize):
i + = pSize - 1
elif (i < len (st)):
res + = st[i]
return res
if __name__ = = "__main__" :
st = "Z-kmalgorithmkmiskmkmkmhelpfulkminkmsearching"
word = "km"
print (goodStr(st, word))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void getZarr( string str, int [] Z)
{
int n = str.Length;
int k;
int L = 0;
int R = 0;
for ( int i = 1; i < n; ++i)
{
if (i > R)
{
L = R = i;
while (R < n && str[R - L] ==
str[R])
{
R++;
}
Z[i] = R - L;
R--;
}
else
{
k = i - L;
if (Z[k] < R - i + 1)
{
Z[i] = Z[k];
}
else
{
L = i;
while (R < n && str[R - L] ==
str[R])
{
R++;
}
Z[i] = R - L;
R--;
}
}
}
}
static string goodStr( string str, string word)
{
string concat = word + "$" + str;
int l = concat.Length;
int []Z = new int [l];
getZarr(concat, Z);
string res= "" ;
int pSize = word.Length;
for ( int i = 0; i < l; ++i) {
if (i + pSize < l - 1 && Z[i + pSize + 1] == pSize) {
i += pSize - 1;
}
else if (i < str.Length) {
res += str[i];
}
}
return res;
}
static public void Main()
{
string str
= "Z-kmalgorithmkmiskmkmkmhelpfulkminkmsearching" ;
string word = "km" ;
Console.WriteLine(goodStr(str, word));
}
}
|
Javascript
<script>
function getZarr(str,Z)
{
let n = str.length;
let k;
let L = 0;
let R = 0;
for (let i = 1; i < n; ++i)
{
if (i > R)
{
L = R = i;
while (R < n && str[R - L] ==
str[R])
{
R++;
}
Z[i] = R - L;
R--;
}
else
{
k = i - L;
if (Z[k] < R - i + 1)
{
Z[i] = Z[k];
}
else
{
L = i;
while (R < n && str[R - L] ==
str[R])
{
R++;
}
Z[i] = R - L;
R--;
}
}
}
}
function goodStr(str,word)
{
let concat = word + "$" + str;
let l = concat.length;
let Z = new Array(l);
getZarr(concat, Z);
let res= "" ;
let pSize = word.length;
for (let i = 0; i < l; ++i) {
if (i + pSize < l - 1 && Z[i + pSize + 1] == pSize) {
i += pSize - 1;
}
else if (i < str.length) {
res += str[i];
}
}
return res;
}
let str = "Z-kmalgorithmkmiskmkmkmhelpfulkminkmsearching" ;
let word = "km" ;
document.write(goodStr(str, word));
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...