-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
algorithms-and-data-structures/src/main/java/com/xtenzq/leetcode/MakeGood.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
algorithms-and-data-structures/src/test/java/com/xtenzq/leetcode/MakeGoodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |