From f422d434deeab08d26ebb51705d80249b80eeba5 Mon Sep 17 00:00:00 2001 From: vitalish Date: Tue, 15 Oct 2024 11:29:22 +0300 Subject: [PATCH] Day 5 :: 1544. Make The String Great --- .../leetcode/stack/MakeTheStringGreat.java | 29 ++++++++++++++++++ .../stack/MakeTheStringGreatTest.java | 30 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/main/java/com/vitalish/leetcode/stack/MakeTheStringGreat.java create mode 100644 src/test/java/com/vitalish/leetcode/stack/MakeTheStringGreatTest.java diff --git a/src/main/java/com/vitalish/leetcode/stack/MakeTheStringGreat.java b/src/main/java/com/vitalish/leetcode/stack/MakeTheStringGreat.java new file mode 100644 index 0000000..0746b91 --- /dev/null +++ b/src/main/java/com/vitalish/leetcode/stack/MakeTheStringGreat.java @@ -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 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()); + } + +} diff --git a/src/test/java/com/vitalish/leetcode/stack/MakeTheStringGreatTest.java b/src/test/java/com/vitalish/leetcode/stack/MakeTheStringGreatTest.java new file mode 100644 index 0000000..4dec796 --- /dev/null +++ b/src/test/java/com/vitalish/leetcode/stack/MakeTheStringGreatTest.java @@ -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() { + return Stream.of( + Arguments.of("aAbBcc", "cc"), + Arguments.of("leEeetcode", "leetcode"), + Arguments.of("abBAcC", ""), + Arguments.of("s", "s") + ); + } + +} \ No newline at end of file