Given a string and a character, remove all occurrences of that character from the string.
- A string
str(1 <= str.length() <= 10000) - A character
chto be removed
- Return a new string with all occurrences of
chremoved
- String contains only printable ASCII characters
- Character
chis a single character - If all characters are removed, return empty string
Input:
str = "abxxcdfexxd"
ch = 'x'
Output:
"abcdfed"
Input:
str = "hello world"
ch = 'l'
Output:
"heo word"
Input:
str = "aaaa"
ch = 'a'
Output:
""
string rmvCharStr(string str, char ch) {
string result;
int i = 0;
while (str[i] != '\0') {
if(str[i] != ch)
result.push_back(str[i]);
i++;
}
return result;
}Time Complexity: O(n)
Space Complexity: O(n)
✓ Works correctly
'\0' check
string rmvCharStr(string str, char ch) {
string result;
for(char c : str) {
if(c != ch)
result.push_back(c);
}
return result;
}Time Complexity: O(n)
Space Complexity: O(n)
✅ Cleaner syntax
✅ No manual indexing
✅ More readable
#include <algorithm>
string rmvCharStr(string str, char ch) {
str.erase(remove(str.begin(), str.end(), ch), str.end());
return str;
}Time Complexity: O(n)
Space Complexity: O(1) - modifies in-place
✅ One-liner!
✅ Standard library approach
✅ Most efficient (no extra allocation)
✅ Shows C++ expertise
How it works:
remove()shifts all non-matching elements to the front- Returns iterator pointing to the "new end"
erase()removes elements from that point to actual end
string rmvCharStr(string str, char ch) {
string result;
result.reserve(str.size()); // Pre-allocate memory
for(char c : str) {
if(c != ch)
result.push_back(c);
}
return result;
}Time Complexity: O(n)
Space Complexity: O(n)
✅ Avoids multiple reallocations
✅ Better performance for large strings
| Approach | Time | Space | Readability | Performance | Interview Score |
|---|---|---|---|---|---|
| Manual Loop | O(n) | O(n) | 6/10 | Good | 6/10 |
| Range-based | O(n) | O(n) | 9/10 | Good | 8/10 |
| STL Remove-Erase | O(n) | O(1) | 7/10 | Best | 10/10 |
| With Reserve | O(n) | O(n) | 8/10 | Very Good | 9/10 |
- Strings in C++ are mutable (unlike Java)
- Can modify in-place using
erase() push_back()may cause reallocations
- Creating new string: O(n) space
- In-place modification: O(1) extra space
reserve()prevents multiple reallocations
remove()- shifts elements, doesn't actually erase- Always pair with
erase()for actual removal - This pattern is called "remove-erase idiom"
- Start simple - show you can solve it
- Optimize - mention STL approach
- Discuss trade-offs - readability vs performance
- Ask clarifications - modify in-place vs new string?
- Remove duplicates from string
- Remove vowels from string
- Filter characters based on condition
- String compression
- Must scan entire string once
- Each character processed exactly once
- New string approach: O(n) - worst case no characters removed
- In-place approach: O(1) - only modifying existing string
✓ Multiple occurrences
✓ Character in middle
✓ Removing all characters
✓ Character not present
✓ Empty string
✓ Single character (match and no match)
✓ Special characters (spaces)