#1544
Easy Algorithms Make the string great
String Stack
68.4% acceptance
Feb 25, 2026
3218
186
Given a string s of lower and upper case English letters.
A good string is a string which doesn't have two adjacent characters s[i] and s[i + 1] where s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.
To make the string good, you can choose two adjacent characters that make the string bad and remove them.
Return the string after making it good.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn make_good(s: String) -> String {
let mut stack: Vec<u8> = Vec::new();
for b in s.bytes() {
if let Some(&top) = stack.last() {
// They're the same letter but different case if their difference is 32
if top != b && top.to_ascii_lowercase() == b.to_ascii_lowercase() {
stack.pop();
continue;
}
}
stack.push(b);
}
String::from_utf8(stack).unwrap()
}
}