Make The String Great

easy string stack

Problem

Given a string s, repeatedly remove any pair of adjacent characters where one is the lowercase form and the other is the uppercase form of the same letter, until no such pair remains. Return the resulting string.

Inputs = "leEeetcode"
Output"leetcode"
"leEeetcode" → "leetcode" after removing 'eE'.

def make_good(s):
    st = []
    for c in s:
        if st and abs(ord(st[-1]) - ord(c)) == 32:
            st.pop()
        else:
            st.append(c)
    return ''.join(st)
function makeGood(s) {
  const st = [];
  for (const c of s) {
    if (st.length && Math.abs(st[st.length - 1].charCodeAt(0) - c.charCodeAt(0)) === 32) {
      st.pop();
    } else st.push(c);
  }
  return st.join('');
}
class Solution {
    public String makeGood(String s) {
        StringBuilder st = new StringBuilder();
        for (char c : s.toCharArray()) {
            int n = st.length();
            if (n > 0 && Math.abs(st.charAt(n - 1) - c) == 32) st.deleteCharAt(n - 1);
            else st.append(c);
        }
        return st.toString();
    }
}
string makeGood(string s) {
    string st;
    for (char c : s) {
        if (!st.empty() && abs(st.back() - c) == 32) st.pop_back();
        else st.push_back(c);
    }
    return st;
}
Time: O(n) Space: O(n)