DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Make The String Great

/**
 * @param {string} s
 * @return {string}
 */
var makeGood = function (s) {
  let stack = [];
  for (let i = 0; i < s.length; i++) {
    let nextChar = s[i];
    if (stack.length === 0) {
      stack.push(s[i]);
    } else if (
      stack[stack.length - 1]?.toLowerCase() === s[i].toLocaleLowerCase()
    ) {
      if (
        (nextChar === nextChar?.toUpperCase() &&
          stack[stack.length - 1] !== stack[stack.length - 1]?.toUpperCase()) ||
        (stack[stack.length - 1] == stack[stack.length - 1]?.toUpperCase() &&
          nextChar !== nextChar?.toUpperCase())
      ) {
        stack.pop();
      } else {
        stack.push(nextChar);
      }
    } else {
      stack.push(nextChar);
    }
  }

  return stack.length > 0 ? stack.join("") : "";
};

console.log(makeGood("lEeeetcode"));

console.log(makeGood("kkdsFuqUfSDKK"));

console.log(makeGood("abBAcC"));

console.log(makeGood("Pp"));

Enter fullscreen mode Exit fullscreen mode

Top comments (0)