Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3a524ef

Browse files
committed
Optimised the solution [Best results]: Have implemented the program to find the final string after removing the all stars.
1 parent 3204820 commit 3a524ef

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

.idea/workspace.xml

Lines changed: 17 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/com/raj/RemovingStarsFromAString.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,23 @@ public class RemovingStarsFromAString {
4040
public static void main(String[] args) {
4141
// Initialization.
4242
String s = "leet**cod*e";
43-
StringBuilder ans = new StringBuilder(s);
4443

4544
// Logic.
46-
s = getStringWithoutStars(ans);
45+
s = getStringWithoutStars(s);
4746

4847
// Display the result.
4948
System.out.println("The final string after removing the each * is: " + s);
5049
}
5150

52-
private static String getStringWithoutStars(StringBuilder s) {
51+
private static String getStringWithoutStars(String s) {
52+
StringBuilder ans = new StringBuilder();
5353
for (int i = 0; i < s.length(); i++) {
54-
if (s.charAt(i) == '*') {
55-
s.delete(i - 1, i + 1);
56-
i -= 2;
54+
if (s.charAt(i) != '*') {
55+
ans.append(s.charAt(i));
56+
} else {
57+
ans.deleteCharAt(ans.length() - 1);
5758
}
5859
}
59-
return s.toString();
60+
return ans.toString();
6061
}
6162
}

0 commit comments

Comments
 (0)