File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
src/main/java/com/thealgorithms/strings Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .thealgorithms .strings ;
2+
3+ /**
4+ * Removes characters affected by '*' in a string.
5+ * Each '*' deletes the closest non-star character to its left.
6+ *
7+ * Example:
8+ * Input: leet**cod*e
9+ * Output: lecoe
10+ */
11+
12+ public final class RemoveStars {
13+
14+ private RemoveStars () {
15+ // Private constructor to prevent instantiation(object creation)
16+ }
17+
18+ public static String removeStars (String s ) {
19+ StringBuilder result = new StringBuilder ();
20+
21+ for (char c : s .toCharArray ()) {
22+ if (c == '*' ) {
23+ if (result .length () > 0 ) {
24+ result .deleteCharAt (result .length () - 1 );
25+ }
26+ } else {
27+ result .append (c );
28+ }
29+ }
30+ return result .toString ();
31+ }
32+ public static void main (String [] args ) {
33+ String example = "leet**cod*e" ;
34+ System .out .println (removeStars (example ));
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments