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.
Input: S = “GeekforGeeks”, C = ‘e’
GeekforGeeks -> GekforGeks
Input: S = “helloWorld”, C = ‘l’
The idea is to traverse the given string from both ends and find the first occurrences of the character C encountered and remove the corresponding occurrences. Finally, print the resultant string.
Below is the implementation of the above approach:
Time Complexity: O(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.
- Remove the first and last character of each word in a string
- Remove first and last character of a string in Java
- Print the first and last character of each word in a String
- Capitalize the first and last character of each word in a string
- Reverse every word of the string except the first and the last character
- How to find the first and last character of a string in Java
- Swap the first and last character of a string in Java
- Program to count occurrence of a given character in a string
- Count occurrence of a given character in a string using Stream API in Java
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Find the Nth occurrence of a character in the given String
- Queries to find the last non-repeating character in the sub-string of a given string
- Generate two output strings depending upon occurrence of character in input string.
- Generate two output strings depending upon occurrence of character in input string in Python
- Minimize the length of string by removing occurrence of only one character
- Minimum operation require to make first and last character same
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Rearrange given string to maximize the occurrence of string t
- Find the last remaining Character in the Binary String according to the given conditions
- Modify given array by incrementing first occurrence of every element by K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.