从给定的句子字符串中删除最后一次出现的单词
给定两个大小分别为N和M的字符串S和W ,任务是从S中删除最后一次出现的W。如果S中没有出现W ,则按原样打印S。
例子:
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].
方法:可以通过遍历字符串S的每个索引i并检查是否存在从索引i开始的子字符串来解决该问题,该子字符串等于字符串W 。请按照以下步骤解决问题:
- 如果N小于M ,则打印S ,因为S中不可能出现W 。
- 将变量i初始化为NM以迭代字符串S 。
- 迭代直到i大于0并执行以下步骤:
- 检查[i, i+M-1]范围内的子字符串是否等于字符串W。如果相等,则从字符串S中删除[i, i+M-1]范围内的子字符串,然后中断。
- 否则,继续。
- 最后,完成上述步骤后,打印字符串S作为答案。
下面是上述方法的实现:
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
输出
This is GeeksFor
时间复杂度: O(M*N)
辅助空间: O(1)