Thursday, June 24, 2021

Published June 24, 2021 by Anonymous with 0 comment

Remove last occurrence of a word from a given sentence string

Remove last occurrence of a word from a given sentence string

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++

#include <bits/stdc++.h>

using namespace std;

  

string removeLastOccurrence(string S, string W, int N,

                            int M)

{

  

    

    if (M > N)

        return S;

  

    

    

    for (int i = N - M; i >= 0; i--) {

        

        

        int flag = 0;

  

        

        for (int j = 0; j < M; j++) {

  

            

            

            if (S[j + i] != W[j]) {

  

                

                flag = 1;

                break;

            }

        }

  

        

        if (flag == 0) {

  

            

            

            for (int j = i; j < N - M; j++)

                S[j] = S[j + M];

  

            

            S.resize(N - M);

            break;

        }

    }

  

    

    return S;

}

int main()

{

    

    string S = "This is GeeksForGeeks";

    string W = "Geeks";

    int N = S.length();

    int M = W.length();

  

    

    cout << removeLastOccurrence(S, W, N, M) << endl;

  

    return 0;

}

Output
This is GeeksFor

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

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes


Adblock test (Why?)


Original page link

Best Cool Tech Gadgets

Top favorite technology gadgets
      edit

0 comments:

Post a Comment