-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_char.cpp
105 lines (99 loc) · 3.93 KB
/
delete_char.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* DELETE SPECIFIED CHARACTERS FROM STRING
*
* This program deletes specified characters from a given string using modern C++ practices.
* It demonstrates three approaches:
* 1. simpleSolution: Iterates through the string and checks for deletion via std::string::find.
* - Time Complexity: O(n*m) where n is the length of the string and m is the length of the deletion set.
* 2. optimalSolution: Uses an unordered_set for O(1) average lookup to quickly determine whether a character should be deleted.
* - Time Complexity: Average O(n + m), which is more efficient for larger inputs.
* 3. alternativeSolution: Leverages std::remove_if with a lambda to remove characters in place.
*
* ASCII Illustration:
*
* Input String: W e a r e s t u d e n t s
* Characters to Delete: a, e, i, o, u
* ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
* Resulting String: W r s t d n t s
*
* Example Input/Output:
* Input: str = "8613761352066", charsToDelete = "1234567890"
* Output: ""
* Explanation: All numeric characters are deleted, resulting in an empty string.
*/
#include <cassert>
#include <iostream>
#include <string>
#include <unordered_set>
#include <algorithm>
// Simple (Brute-force) Solution
std::string simpleSolution(const std::string& input, const std::string& charsToDelete) {
std::string result;
for (char c : input) {
if (charsToDelete.find(c) == std::string::npos) {
result.push_back(c);
}
}
return result;
}
// Optimal (Efficient) Solution
std::string optimalSolution(const std::string& input, const std::string& charsToDelete) {
std::unordered_set<char> deleteSet(charsToDelete.begin(), charsToDelete.end());
std::string result;
result.reserve(input.size()); // Reserve memory to reduce reallocations
for (char c : input) {
if (deleteSet.find(c) == deleteSet.end()) {
result.push_back(c);
}
}
return result;
}
// (Optional) Alternative Solution using std::remove_if
std::string alternativeSolution(const std::string& input, const std::string& charsToDelete) {
std::unordered_set<char> deleteSet(charsToDelete.begin(), charsToDelete.end());
std::string result = input;
result.erase(std::remove_if(result.begin(), result.end(), [&deleteSet](char c) {
return deleteSet.find(c) != deleteSet.end();
}), result.end());
return result;
}
// Test cases for correctness
void test() {
{
std::string input = "We are students";
std::string charsToDelete = "aeiou";
std::string expected = "W r stdnts";
assert(simpleSolution(input, charsToDelete) == expected);
assert(optimalSolution(input, charsToDelete) == expected);
assert(alternativeSolution(input, charsToDelete) == expected);
}
{
std::string input = "We are students";
std::string charsToDelete = "wxyz";
std::string expected = "We are students";
assert(simpleSolution(input, charsToDelete) == expected);
assert(optimalSolution(input, charsToDelete) == expected);
assert(alternativeSolution(input, charsToDelete) == expected);
}
{
std::string input = "8613761352066";
std::string charsToDelete = "1234567890";
std::string expected = "";
assert(simpleSolution(input, charsToDelete) == expected);
assert(optimalSolution(input, charsToDelete) == expected);
assert(alternativeSolution(input, charsToDelete) == expected);
}
{
std::string input = "119911";
std::string charsToDelete = "";
std::string expected = "119911";
assert(simpleSolution(input, charsToDelete) == expected);
assert(optimalSolution(input, charsToDelete) == expected);
assert(alternativeSolution(input, charsToDelete) == expected);
}
std::cout << "All tests passed!\n";
}
int main() {
test();
return 0;
}