Loading

LeetCode problem#1544

Make The String Great

Solved in 1 languageJava

Solve it on LeetCode(opens in a new tab)

Solution

24 lines
1class Solution {
2    public String makeGood(String s) {
3        StringBuilder sb = new StringBuilder(s);
4
5        for (int i = 0; i < sb.length() - 1; )
6        {
7            int diff = sb.codePointAt(i) - sb.codePointAt(i + 1);
8            if (diff == 32 || diff == -32)
9            {
10                sb.delete(i, i + 2);
11                if (i > 0)
12                {
13                    i--;
14                }
15            }
16            else
17            {
18                i++;
19            }
20        }
21
22        return sb.toString();
23    }
24}

Want a walkthrough of this Java solution?

Get a step-by-step explanation of the idea behind the code.

All problems

Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.