-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveStars.java
76 lines (62 loc) · 2.24 KB
/
RemoveStars.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
public class RemoveStars {
public static void main(String[] args) {
System.out.println("REMOVE ** E : lecoe "+removeStars("leet**cod*e"));
System.out.println("REMOVE ** E : \"\" "+removeStars("qawsedrftgyh************"));
System.out.println("REMOVE ** E : PRUKH "+removeStars("PRAN*S**UKH")); // PRAS**UKH -> PRA*UKH -> PRUKH
System.out.println("REMOVE ** E : LongString "+removeStars("")); // PRAS**UKH -> PRA*UKH -> PRUKH
}
public static String removeStars(String text){
BufferedReader reader;
int[] kk = new int[10000];
int jj = 0;
try {
reader = new BufferedReader(new FileReader("C:\\Users\\pran.sukh\\Downloads\\GOLong\\JAVA\\LEETCODE\\CrackInterviewIn75QA\\longString.txt"));
String line = reader.readLine();
while (line != null) {
text+= line.toString();
// read next line
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
String[] array = text.split("");
int left = 0;
int right = 1;
int lastRemovedFrom =0;
boolean recentlyRemoved = false;
while (right<array.length){
if(array[right].equalsIgnoreCase("*")){
if(recentlyRemoved){
lastRemovedFrom=lastRemovedFrom-1;
while (array[lastRemovedFrom] == ""){
lastRemovedFrom--;
}
array[lastRemovedFrom] = "";
array[right] ="";
}else{
left = right;
// remove
array[left-1] = "";
array[right] ="";
lastRemovedFrom = left-1;
recentlyRemoved = true;
}
}else{
recentlyRemoved = false;
}
right+=1;
}
String result = "";
for(String data : array){
result+=data;
}
return result;
}
}