-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicates.java
More file actions
73 lines (66 loc) · 1.41 KB
/
duplicates.java
File metadata and controls
73 lines (66 loc) · 1.41 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
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
// changing duplicates
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Duplicates {
public static void main(String[] args) {
List<String> zippedList = new ArrayList<>();
zippedList=changeDuplicates(strList);
}
static List<String> changeDuplicates(List<String> list) {
String prev="";
int k=0;
int cnt=1;
List<String> newList = new ArrayList<>();
for(int i=0; i<list.size(); i++) {
if(!prev.equals(list.get(i))) {
cnt=1;
newList.add(list.get(i));
k++;
prev = list.get(i);
} else {
cnt++;
StringBuilder strBuf = new StringBuilder(list.get(i));
strBuf.insert(0, cnt+"#");
newList.remove(newList.size()-1);
newList.add(strBuf.toString());
}
}
return newList;
}
private static String getCompressedLine(String line) {
StringBuilder sb = new StringBuilder();
char prev = line.charAt(0);
int count = 1;
for(int i=1; i<line.length(); i++) {
char ch = line.charAt(i);
if(ch == prev) {
count++;
} else {
if(count > 2) {
//compress
sb.append(count);
sb.append(prev);
} else {
for(int j=0; j<count; j++) {
sb.append(prev);
}
}
//init
prev = ch;
count = 1;
}
}
//last character
if(count > 2) {
//compress
sb.append(count);
sb.append(prev);
} else {
for(int j=0; j<count; j++) {
sb.append(prev);
}
}
return sb.toString();
}
}