Skip to content

Commit

Permalink
Day 5 :: 1544. Make The String Great
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalish committed Oct 15, 2024
1 parent 27457ab commit f422d43
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/java/com/vitalish/leetcode/stack/MakeTheStringGreat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.vitalish.leetcode.stack;

import java.util.Stack;
import java.util.stream.Collectors;

/**
* 1544. Make The String Great
*
* @see https://leetcode.com/problems/make-the-string-great/description/
*/
public class MakeTheStringGreat {

public String makeGood(String s) {
Stack<Character> symbols = new Stack<>();

for (int i = 0; i < s.length(); i++) {
var currentSymbol = s.charAt(i);
if (!symbols.isEmpty() && Math.abs(symbols.peek() - currentSymbol) == 32) {
symbols.pop();
} else {
symbols.push(currentSymbol);
}
}
return symbols.stream()
.map(Object::toString)
.collect(Collectors.joining());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.vitalish.leetcode.stack;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

class MakeTheStringGreatTest {

private static final MakeTheStringGreat makeTheStringGreat = new MakeTheStringGreat();

@ParameterizedTest
@MethodSource("arguments")
void makesTheStringGreat(String input, String expected) {
assertEquals(expected, makeTheStringGreat.makeGood(input));
}

private static Stream<Arguments> arguments() {
return Stream.of(
Arguments.of("aAbBcc", "cc"),
Arguments.of("leEeetcode", "leetcode"),
Arguments.of("abBAcC", ""),
Arguments.of("s", "s")
);
}

}

0 comments on commit f422d43

Please sign in to comment.