Skip to content

Latest commit

 

History

History
48 lines (32 loc) · 747 Bytes

File metadata and controls

48 lines (32 loc) · 747 Bytes

Make The String Great

Problem Link

https://leetcode.com/problems/make-the-string-great/


Pattern

  • Stack
  • String

Approach

Use a stack to remove adjacent characters that are the same letter but different case.


Time Complexity

O(n)

Space Complexity

O(n)


Java Solution

import java.util.*;
class Solution {
    public String makeGood(String s) {
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (!stack.isEmpty() && Math.abs(stack.peek() - c) == 32) stack.pop();
            else stack.push(c);
        }
        StringBuilder ans = new StringBuilder();
        for (char c : stack) ans.append(c);
        return ans.toString();
    }
}