forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveStars.java
More file actions
29 lines (24 loc) · 787 Bytes
/
RemoveStars.java
File metadata and controls
29 lines (24 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.thealgorithms.strings;
/**
* Removes stars from the input string by deleting the closest non-star character
* to the left of each star along with the star itself.
*
* @param s1 The input string containing stars (*)
* @return The string after all stars and their closest left characters are removed
*/
public class RemoveStars {
public static String removeStars(String s1) {
StringBuilder sc = new StringBuilder();
for (int i = 0; i < s1.length(); i++) {
char ch = s1.charAt(i);
if (ch == '*') {
if (sc.length() > 0) {
sc.deleteCharAt(sc.length() - 1);
}
} else {
sc.append(ch);
}
}
return sc.toString();
}
}