Skip to content

Commit

Permalink
1544. Make The String Great
Browse files Browse the repository at this point in the history
  • Loading branch information
xtenzQ committed Aug 2, 2024
1 parent 7900d4c commit 60f2282
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.xtenzq.leetcode;

import java.util.Stack;

public class MakeGood {

public static String makeGood(String s) {
if (s.length() == 1) {
return s;
}
Stack<Character> chars = new Stack<>();
chars.push(s.charAt(0));
for (int i = 1; i < s.length(); i++) {
if (!chars.empty() && Math.abs((int)s.charAt(i) - (int)chars.peek()) == 32) {
chars.pop();
} else {
chars.push(s.charAt(i));
}
}

StringBuilder sb = new StringBuilder();
for (char c : chars) {
sb.append(c);
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.xtenzq.leetcode;

import org.junit.jupiter.api.Test;

import static com.xtenzq.leetcode.MakeGood.makeGood;
import static org.junit.jupiter.api.Assertions.*;

class MakeGoodTest {

@Test
void makeGood_case1() {
assertEquals("leetcode", makeGood("leEeetcode"));
}

@Test
void makeGood_case2() {
assertEquals("", makeGood("abBAcC"));
}

@Test
void makeGood_case3() {
assertEquals("s", makeGood("s"));
}
}

0 comments on commit 60f2282

Please sign in to comment.