Skip to content

Commit ba5767d

Browse files
authored
Create 1544. Make The String Great (#448)
2 parents b5faba1 + 2cc3321 commit ba5767d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

1544. Make The String Great

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
};

0 commit comments

Comments
 (0)