Given a character C and a string S , the task is to remove the first and last occurrence of the character C from the string S .
Examples:
Input: S = “GeekforGeeks”, C = ‘e’ Output: GeksforGeks Explanation: G e ekforGe e ks -> GekforGeks
Input: S = “helloWorld”, C = ‘l’ Output: heloWord
Approach: The idea is to traverse the given string from both ends and find the first occurrence of the character C encountered and remove the corresponding occurrences. Finally, print the resultant string.
Define a function named removeOcc , which accepts a reference to a string s and a character ch as input parameters. Traverse the string s from the beginning using a for loop and check if each character matches with the given character ch . If the character is found, erase the first occurrence of that character using the erase function and break the loop. Traverse the string s from the end using another for loop and check if each character matches with the given character ch . If the character is found, erase the last occurrence of that character using the erase function and break the loop. Return the modified string s . In the main function, define a string s and initialize it with “ hello world “. Define a character ch and initialize it with ‘ l ‘. Call the removeOcc function and pass the string s and character ch as arguments. Print the modified string s using cout . Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string removeOcc(string& s, char ch)
{
for (int i = 0; s[i]; i++) {
if (s[i] == ch) {
s.erase(s.begin() + i);
break;
}
}
for (int i = s.length() - 1;
i > -1; i--) {
if (s[i] == ch) {
s.erase(s.begin() + i);
break;
}
}
return s;
}
int main()
{
string s = "hello world";
char ch = 'l';
cout << removeOcc(s, ch);
return 0;
}
Java
class GFG{
static String removeOcc(String s, char ch)
{
for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) == ch)
{
s = s.substring(0, i) +
s.substring(i + 1);
break;
}
}
for (int i = s.length() - 1; i > -1; i--)
{
if (s.charAt(i) == ch)
{
s = s.substring(0, i) +
s.substring(i + 1);
break;
}
}
return s;
}
public static void main(String[] args)
{
String s = "hello world";
char ch = 'l';
System.out.print(removeOcc(s, ch));
}
}
Python3
def removeOcc(s, ch):
for i in range(len(s)):
if (s[i] == ch):
s = s[0 : i] + s[i + 1:]
break
for i in range(len(s) - 1, -1, -1):
if (s[i] == ch):
s = s[0 : i] + s[i + 1:]
break
return s
s = "hello world"
ch = 'l'
print(removeOcc(s, ch))
C#
using System;
class GFG{
static String removeOcc(String s, char ch)
{
for (int i = 0; i < s.Length; i++)
{
if (s[i] == ch)
{
s = s.Substring(0, i) +
s.Substring(i + 1);
break;
}
}
for (int i = s.Length - 1; i > -1; i--)
{
if (s[i] == ch)
{
s = s.Substring(0, i) +
s.Substring(i + 1);
break;
}
}
return s;
}
public static void Main(String[] args)
{
String s = "hello world";
char ch = 'l';
Console.Write(removeOcc(s, ch));
}
}
Javascript
<script>
function removeOcc(s, ch) {
for (var i = 0; i < s.length; i++) {
if (s[i] === ch) {
s = s.substring(0, i) + s.substring(i + 1);
break;
}
}
for (var i = s.length - 1; i > -1; i--) {
if (s[i] === ch) {
s = s.substring(0, i) + s.substring(i + 1);
break;
}
}
return s;
}
var s = "hello world";
var ch = "l";
document.write(removeOcc(s, ch));
</script>
Time Complexity: O(N) Auxiliary Space: O(1)
Method #2: Using Index Method Convert the string into a list. Traverse the list and if we find a given character, then remove that index using pop() and break the loop Traverse the list from the end and if we find a given character, then remove that index using pop() and break the loop. Join the list and print it. Below is the implementation:
C++
#include <iostream>
#include <string>
using namespace std;
string removeOcc(string str, char ch) {
string s = str;
for (int i = 0; i < s.length(); i++) {
if (s[i] == ch) {
s.erase(i, 1);
break;
}
}
for (int i = s.length() - 1; i >= 0; i--) {
if (s[i] == ch) {
s.erase(i, 1);
break;
}
}
return s;
}
int main() {
string s = "hello world";
char ch = 'l';
cout << removeOcc(s, ch) << endl;
return 0;
}
Java
import java.util.ArrayList;
import java.util.List;
public class RemoveOccurrence {
static String removeOcc(String str, char ch) {
List<Character> s = new ArrayList<>();
for (int i = 0; i < str.length(); i++) {
s.add(str.charAt(i));
}
for (int i = 0; i < s.size(); i++) {
if (s.get(i) == ch) {
s.remove(i);
break;
}
}
for (int i = s.size() - 1; i >= 0; i--) {
if (s.get(i) == ch) {
s.remove(i);
break;
}
}
StringBuilder sb = new StringBuilder();
for (char c : s) {
sb.append(c);
}
return sb.toString();
}
public static void main(String[] args) {
String s = "hello world";
char ch = 'l';
System.out.println(removeOcc(s, ch));
}
}
Python3
def removeOcc(str, ch):
s = list(str)
for i in range(len(s)):
if(s[i] == ch):
s.pop(i)
break
for i in range(len(s)-1, -1, -1):
if(s[i] == ch):
s.pop(i)
break
return ''.join(s)
s = "hello world"
ch = 'l'
print(removeOcc(s, ch))
C#
using System;
class Program {
static string RemoveOcc(string str, char ch) {
string s = str;
for (int i = 0; i < s.Length; i++) {
if (s[i] == ch) {
s = s.Remove(i, 1);
break;
}
}
for (int i = s.Length - 1; i >= 0; i--) {
if (s[i] == ch) {
s = s.Remove(i, 1);
break;
}
}
return s;
}
static void Main(string[] args) {
string s = "hello world";
char ch = 'l';
Console.WriteLine(RemoveOcc(s, ch));
}
}
Javascript
<script>
function removeOcc(str, ch)
{
s = str.split('')
for(let i = 0; i < s.length; i++)
{
if(s[i] == ch){
s.splice(i, 1)
break
}
}
for(let i = s.length - 1; i >= 0; i--)
{
if(s[i] == ch)
{
s.splice(i, 1)
break
}
}
return s.join('')
}
let s = "hello world"
let ch = 'l'
document.write(removeOcc(s, ch))
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule. Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
02 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...