Skip to content

Commit

Permalink
Day 10 :: 2390. Removing Stars From a String
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalish committed Oct 17, 2024
1 parent d8e4a70 commit 49c90cc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.vitalish.leetcode.stack;

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

/**
* 2390. Removing Stars From a String
*
* @see https://leetcode.com/problems/removing-stars-from-a-string/description/
*/
public class RemovingStarsFromString {

public String removeStars(String s) {
Stack<Character> stack = new Stack<>();

for (int i = 0; i < s.length(); i++) {
var current = s.charAt(i);
if (current == '*') {
stack.pop();
} else {
stack.push(current);
}
}

return stack.stream()
.map(String::valueOf)
.collect(Collectors.joining());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 RemovingStarsFromStringTest {

RemovingStarsFromString removingStarsFromString = new RemovingStarsFromString();

@ParameterizedTest
@MethodSource("arguments")
void removesStarsFromString(String input, String expected) {
assertEquals(expected, removingStarsFromString.removeStars(input));
}

private static Stream<Arguments> arguments() {
return Stream.of(
Arguments.of("leet**cod*e", "lecoe"),
Arguments.of("erase*****", "")
);
}
}

0 comments on commit 49c90cc

Please sign in to comment.