-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution3.java
More file actions
64 lines (57 loc) · 2.07 KB
/
Solution3.java
File metadata and controls
64 lines (57 loc) · 2.07 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
/**
* Problem statement:
* Given a string and a chunk size, perform deduplication on it. The process of deduplication returns an
* intermediate string. The intermediate string is then used to perform reduplication. The process of
* reduplication gives back the original string.
*
* Steps to be followed:
* 1. Break the string into chunks of the given size (Values of chunk size can be 1KB,10KB and so on).
* 2. Find the unique chunks and make a note of where these chunks occur in the string.
* 3. The intermediate string should contain the unique strings and their positions.
* 4. This string alone should be used to perform reduplication, which constructs the original string.
*
* Input:
* abcdexyzvwabcde
* chunk size: 5 bytes
*
* Output after deduplication:
* abcde-0-2,xyzvw1
*
* Output after reduplication:
* abcdexyzvwabcde
*/
import java.util.HashMap;
public class Solution3 {
static String dedup(String inputStr, int chunkSize) {
int chunks = 0;
StringBuilder dedup = new StringBuilder();
for(int i = 0; i< inputStr.length();){
String str = inputStr.substring(i,i+chunkSize);
if(dedup.indexOf(str) != -1){
int ind = dedup.indexOf(str);
dedup.insert(dedup.indexOf(",", ind), "-"+chunks);
}
else{
dedup.append(str+"-"+chunks+",");
}
chunks++;
i+=chunkSize;
}
return dedup.toString();
}
static String redup(String deduplicatedStr, int chunkSize) {
String str = "";
for(int i = 0; i< deduplicatedStr.length();i++) {
String str1 = deduplicatedStr.substring(i, i + chunkSize);
int chunkIndex = deduplicatedStr.indexOf(",",i+chunkSize);
String chunk = deduplicatedStr.substring(i+chunkSize, chunkIndex);
System.out.println(chunk);
}
return "";
}
public static void main(String[] args) {
String dedupStr = dedup("abcdeabcdfabcde", 5);
System.out.println(dedupStr);
//redup(dedupStr, 5);
}
}