# Remove last occurrence of a word from a given sentence string

• Difficulty Level : Medium
• Last Updated : 10 Mar, 2022

Given two strings S and W of sizes N and M respectively, the task is to remove the last occurrence of W from S. If there is no occurrence of W in S, print S as it is.

Examples:

Input: S = “This is GeeksForGeeks”, W=”Geeks”
Output: This is GeeksFor
Explanation:
The last occurrence of “Geeks” in the string is substring over the range [16, 20].

Input: S=”Hello World”, W=”Hell”
Output: o World
Explanation:
The last occurrence of “Hell” in the string is substring over the range [0, 3].

Approach: The problem can be solved by iterating over every index i of string S and checking if there is a substring starting from the index i, which is equal to string W. Follow the steps below to solve the problem:

• If N is smaller than M, print S, as there can be no occurrence of W in S.
• Initialize a variable i as N-M to iterate over the string S.
• Iterate until i is greater than 0 and perform the following steps:
• Check whether the substring over the range [i, i+M-1] is equal to string W or not. If it is equal, then remove the substring over the range [i, i+M-1] from string S and then break.
• Otherwise, continue.
• Finally, after completing the above steps, print the string S as the answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to remove last occurrence``// of W from S``string removeLastOccurrence(string S, string W, ``int` `N,``                            ``int` `M)``{` `    ``// If M is greater than N``    ``if` `(M > N)``        ``return` `S;` `    ``// Iterate while i is greater than``    ``// or equal to 0``    ``for` `(``int` `i = N - M; i >= 0; i--) {``        ``// Stores if occurrence of W has``        ``// been found or not``        ``int` `flag = 0;` `        ``// Iterate over the range [0, M]``        ``for` `(``int` `j = 0; j < M; j++) {` `            ``// If S[j+1] is not equal to``            ``// W[j]``            ``if` `(S[j + i] != W[j]) {` `                ``// Mark flag true and break``                ``flag = 1;``                ``break``;``            ``}``        ``}` `        ``// If occurrence has been found``        ``if` `(flag == 0) {` `            ``// Delete the substring over the``            ``// range [i, i+M]``            ``for` `(``int` `j = i; j < N - M; j++)``                ``S[j] = S[j + M];` `            ``// Resize the string S``            ``S.resize(N - M);``            ``break``;``        ``}``    ``}` `    ``// Return S``    ``return` `S;``}``// Driver Code``int` `main()``{``    ``// Input``    ``string S = ``"This is GeeksForGeeks"``;``    ``string W = ``"Geeks"``;``    ``int` `N = S.length();``    ``int` `M = W.length();` `    ``// Function call``    ``cout << removeLastOccurrence(S, W, N, M) << endl;` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{``    ` `// Function to remove last occurrence``// of W from S``static` `String removeLastOccurrence(String S, String W,``                                   ``int` `N, ``int` `M)``{``    ` `    ``// If M is greater than N``    ``char``[] ch = S.toCharArray();``    ``if` `(M > N)``        ``return` `S;` `    ``// Iterate while i is greater than``    ``// or equal to 0``    ``for``(``int` `i = N - M; i >= ``0``; i--)``    ``{``        ` `        ``// Stores if occurrence of W has``        ``// been found or not``        ``int` `flag = ``0``;` `        ``// Iterate over the range [0, M]``        ``for``(``int` `j = ``0``; j < M; j++)``        ``{``            ` `            ``// If S[j+1] is not equal to``            ``// W[j]``            ``if` `(ch[j + i] != W.charAt(j))``            ``{``                ` `                ``// Mark flag true and break``                ``flag = ``1``;``                ``break``;``            ``}``        ``}` `        ``// If occurrence has been found``        ``if` `(flag == ``0``)``        ``{``            ` `            ``// Delete the substring over the``            ``// range [i, i+M]``            ``for``(``int` `j = i; j < N - M; j++)``                ``ch[j] = ch[j + M];``                ` `            ``break``;``        ``}``    ``}``    ` `    ``char``[] chh = ``new` `char``[N - M];``    ` `    ``// Resize the string S``    ``for``(``int` `i = ``0``; i < N - M; i++)``    ``{``        ``chh[i] = ch[i];``    ``}``    ` `    ``// Return S``    ``return` `String.valueOf(chh);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Input``    ``String S = ``"This is GeeksForGeeks"``;``    ``String W = ``"Geeks"``;``    ``int` `N = S.length();``    ``int` `M = W.length();` `    ``// Function call``    ``System.out.print(removeLastOccurrence(S, W, N, M));``}``}` `// This code is contributed by sanjoy_62`

## Python3

 `# Python3 program for the above approach` `# Function to remove last occurrence``# of W from S``def` `removeLastOccurrence(S, W, N, M):``    ``S ``=` `[i ``for` `i ``in` `S]``    ``W ``=` `[i ``for` `i ``in` `W]` `    ``# If M is greater than N``    ``if` `(M > N):``        ``return` `S` `    ``# Iterate while i is greater than``    ``# or equal to 0``    ``for` `i ``in` `range``(N ``-` `M, ``-``1``, ``-``1``):``        ``# of W has``        ``# been found or not``        ``flag ``=` `0` `        ``# Iterate over the range [0, M]``        ``for` `j ``in` `range``(M):``            ``# If S[j+1] is not equal to``            ``# W[j]``            ``if` `(S[j ``+` `i] !``=` `W[j]):` `                ``# Mark flag true and break``                ``flag ``=` `1``                ``break` `        ``# If occurrence has been found``        ``if` `(flag ``=``=` `0``):` `            ``# Delete the subover the``            ``# range [i, i+M]``            ``for` `j ``in` `range``(i,N``-``M):``                ``S[j] ``=` `S[j ``+` `M]` `            ``# Resize the S``            ``S ``=` `S[:N ``-` `M]``            ``break` `    ``# Return S``    ``return` `"".join(S)``  ` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``# Input``    ``S ``=` `"This is GeeksForGeeks"``    ``W ``=` `"Geeks"``    ``N ``=` `len``(S)``    ``M ``=` `len``(W)` `    ``# Function call``    ``print` `(removeLastOccurrence(S, W, N, M))` `# This code is contributed by mohit kumar 29.`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{``  ` `// Function to remove last occurrence``// of W from S``static` `string` `removeLastOccurrence(``string` `S, ``string` `W, ``int` `N,``                            ``int` `M)``{` `    ``// If M is greater than N``   ``char``[] ch = S.ToCharArray();``    ``if` `(M > N)``        ``return` `S;` `    ``// Iterate while i is greater than``    ``// or equal to 0``    ``for` `(``int` `i = N - M; i >= 0; i--)``    ``{``      ` `        ``// Stores if occurrence of W has``        ``// been found or not``        ``int` `flag = 0;` `        ``// Iterate over the range [0, M]``        ``for` `(``int` `j = 0; j < M; j++) {` `            ``// If S[j+1] is not equal to``            ``// W[j]``            ``if` `(ch[j + i] != W[j]) {` `                ``// Mark flag true and break``                ``flag = 1;``                ``break``;``            ``}``        ``}` `        ``// If occurrence has been found``        ``if` `(flag == 0) {` `            ``// Delete the substring over the``            ``// range [i, i+M]``            ``for` `(``int` `j = i; j < N - M; j++)``                ``ch[j] = ch[j + M];` `            ``// Resize the string S``            ``Array.Resize(``ref` `ch,N - M);``            ``break``;``        ``}``    ``}``     ``S = ``string``.Concat(ch);``  ` `    ``// Return S``    ``return` `S;``}` `// Driver Code``public` `static` `void` `Main()``{``    ``// Input``    ``string` `S = ``"This is GeeksForGeeks"``;``    ``string` `W = ``"Geeks"``;``    ``int` `N = S.Length;``    ``int` `M = W.Length;` `    ``// Function call``    ``Console.Write(removeLastOccurrence(S, W, N, M));``}``}` `// This code is contributed by bgangwar59.`

## Javascript

 ``

Output

`This is GeeksFor`

Time Complexity: O(M*N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up