File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ std::string makeGood(std::string s) {
4
+ int endPosition = 0; // Represents the end position of the modified string
5
+ char charArray[s.size()]; // Convert the string to a character array
6
+
7
+ // Convert the string to a character array
8
+ for (int i = 0; i < s.size(); ++i) {
9
+ charArray[i] = s[i];
10
+ }
11
+
12
+ // Loop through each character in the string
13
+ for (int currentPosition = 0; currentPosition < s.size(); currentPosition++) {
14
+ // Check if the current character can be removed
15
+ if (endPosition > 0 && abs(charArray[currentPosition] - charArray[endPosition - 1]) == 32)
16
+ endPosition--; // Decrement the end position if the current character can be removed
17
+ else {
18
+ // Otherwise, keep the current character and increment the end position
19
+ charArray[endPosition] = charArray[currentPosition];
20
+ endPosition++;
21
+ }
22
+ }
23
+
24
+ // Convert the modified character array to a string and return only the valid portion
25
+ return std::string(charArray, charArray + endPosition);
26
+ }
27
+ };
You can’t perform that action at this time.
0 commit comments