Skip to content

Create 1544. Make The String Great #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions 1544. Make The String Great
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
std::string makeGood(std::string s) {
int endPosition = 0; // Represents the end position of the modified string
char charArray[s.size()]; // Convert the string to a character array

// Convert the string to a character array
for (int i = 0; i < s.size(); ++i) {
charArray[i] = s[i];
}

// Loop through each character in the string
for (int currentPosition = 0; currentPosition < s.size(); currentPosition++) {
// Check if the current character can be removed
if (endPosition > 0 && abs(charArray[currentPosition] - charArray[endPosition - 1]) == 32)
endPosition--; // Decrement the end position if the current character can be removed
else {
// Otherwise, keep the current character and increment the end position
charArray[endPosition] = charArray[currentPosition];
endPosition++;
}
}

// Convert the modified character array to a string and return only the valid portion
return std::string(charArray, charArray + endPosition);
}
};
Loading