-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRemovingStarsFromAString.java
More file actions
42 lines (40 loc) · 1.25 KB
/
RemovingStarsFromAString.java
File metadata and controls
42 lines (40 loc) · 1.25 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
/*https://leetcode.com/problems/removing-stars-from-a-string/*/
class Solution {
public String removeStars(String s) {
StringBuilder build = new StringBuilder(s);
int S = s.length(), i = 0, j = 0, loop = 0, consecStarCount = 0, newI;
while (i < build.length())
{
if (build.charAt(i) == '*') ++consecStarCount;
if (build.charAt(i) != '*' || i+1 == build.length())
{
if (consecStarCount > 0)
{
j = i+1 == build.length() && build.charAt(i) == '*' ? i : i-1;
newI = j-(2*consecStarCount)+1;
build.delete(newI,j+1);
i = newI-1;
consecStarCount = 0;
}
}
++i;
}
return build.toString();
}
}
class Solution {
public String removeStars(String s) {
int S = s.length(), i = 0;
char[] arr = new char[S];
for (char ch : s.toCharArray())
{
if (ch == '*')
--i;
else arr[i++] = ch;
}
StringBuilder build = new StringBuilder("");
for (int j = 0; j < i; ++j)
build.append(arr[j]);
return build.toString();
}
}