LeetCode Entry
3561. Resulting String After Adjacent Removals
Remove leftmost abc-adjucent pairs
3561. Resulting String After Adjacent Removals medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1563
Problem TLDR
Remove leftmost abc-adjucent pairs
Intuition
Use stack
Approach
- Rust: split_off clears the string
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
fun resultingString(s: String) = buildString {
for (c in s) if (any() && abs(last() - c) % 24 == 1)
deleteAt(lastIndex) else append(c)
}
pub fn resulting_string(mut s: String) -> String {
for c in s.split_off(0).bytes() {
if s.ends_with(|p| c.abs_diff(p as _) % 24 == 1) { s.pop(); } else { s.push(c as _) }
}; s
}
Comments