📌  相关文章
📜  从字符串中删除给定字符的第一个和最后一次出现

📅  最后修改于: 2021-04-22 00:38:49             🧑  作者: Mango

给定一个字符C和一个字符串S ,任务是从字符串S中删除字符C的第一次出现和最后一次出现。
例子:

方法:
想法是从两端遍历给定的字符串,找到遇到的字符C的第一个匹配项,并删除相应的匹配项。最后,打印结果字符串。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
  
// Function to remove first and last
// occurrence of a given character
// from the given string
string removeOcc(string& s, char ch)
{
    // Traverse the given string from
    // the beginning
    for (int i = 0; s[i]; i++) {
  
        // If ch is found
        if (s[i] == ch) {
            s.erase(s.begin() + i);
            break;
        }
    }
  
    // Traverse the given string from
    // the end
    for (int i = s.length() - 1;
        i > -1; i--) {
  
        // If ch is found
        if (s[i] == ch) {
            s.erase(s.begin() + i);
            break;
        }
    }
  
    return s;
}
  
// Driver Code
int main()
{
    string s = "hello world";
  
    char ch = 'l';
  
    cout << removeOcc(s, ch);
    return 0;
}


Java
// Java Program to implement
// the above approach
class GFG{
  
// Function to remove first and last
// occurrence of a given character
// from the given String
static String removeOcc(String s, char ch)
{
    // Traverse the given String from
    // the beginning
    for (int i = 0; i < s.length(); i++) 
    {
  
        // If ch is found
        if (s.charAt(i) == ch) 
        {
            s = s.substring(0, i) + 
                s.substring(i + 1);
            break;
        }
    }
  
    // Traverse the given String from
    // the end
    for (int i = s.length() - 1; i > -1; i--)
    {
  
        // If ch is found
        if (s.charAt(i) == ch) 
        {
            s = s.substring(0, i) + 
                s.substring(i + 1);
            break;
        }
    }
    return s;
}
  
// Driver Code
public static void main(String[] args)
{
    String s = "hello world";
  
    char ch = 'l';
  
    System.out.print(removeOcc(s, ch));
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 program to implement
# the above approach
  
# Function to remove first and last
# occurrence of a given character
# from the given string
def removeOcc(s, ch):
      
    # Traverse the given string from
    # the beginning
    for i in range(len(s)):
  
        # If ch is found
        if (s[i] == ch):
            s = s[0 : i] + s[i + 1:]
            break
      
    # Traverse the given string from
    # the end
    for i in range(len(s) - 1, -1, -1):
  
        # If ch is found
        if (s[i] == ch):
            s = s[0 : i] + s[i + 1:]
            break
  
    return s
  
# Driver Code
s = "hello world"
  
ch = 'l'
  
print(removeOcc(s, ch))
  
# This code is contributed by sanjoy_62


C#
// C# Program to implement
// the above approach
using System;
class GFG{
  
// Function to remove first and last
// occurrence of a given character
// from the given String
static String removeOcc(String s, char ch)
{
    // Traverse the given String from
    // the beginning
    for (int i = 0; i < s.Length; i++) 
    {
  
        // If ch is found
        if (s[i] == ch) 
        {
            s = s.Substring(0, i) + 
                s.Substring(i + 1);
            break;
        }
    }
  
    // Traverse the given String from
    // the end
    for (int i = s.Length - 1; i > -1; i--)
    {
  
        // If ch is found
        if (s[i] == ch) 
        {
            s = s.Substring(0, i) + 
                s.Substring(i + 1);
            break;
        }
    }
    return s;
}
  
// Driver Code
public static void Main(String[] args)
{
    String s = "hello world";
  
    char ch = 'l';
  
    Console.Write(removeOcc(s, ch));
}
}
  
// This code is contributed by sapnasingh4991


输出:
helo word

时间复杂度: O(N)
辅助空间: O(1)