Repository navigation
Expand file tree
/
Copy pathcompression.java
More file actions
44 lines (43 loc) · 1.72 KB
/
Copy pathcompression.java
File metadata and controls
44 lines (43 loc) · 1.72 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
public class compression {
public static String findCompressedString(String s, int n) {
Integer count = 1;
Character lastChar = s.charAt(0);
StringBuilder c = new StringBuilder("");
for (int i = 1; i < n; i++) {
// New Character found and it is NOT Last character of the String.
if (s.charAt(i) != lastChar && i != n - 1) {
c.append(lastChar.toString());
if (count != 1)
c.append(count.toString());
count = 1;
}
// Last character reached and is same as previous characters -> Multiple Occurence
// of Last character
else if (i == n - 1 && s.charAt(i) == lastChar) {
c.append(lastChar.toString());
count++;
if (count != 1)
c.append(count.toString());
}
// Last character reached and is different from previous characters -> Single Occurence
// of Last character.
else if (i == n - 1 && s.charAt(i) != lastChar) {
c.append(lastChar.toString());
if (count != 1)
c.append(count.toString());
c.append(s.charAt(i));
}
// New Character not found, just increase count of old lastChar, Last character NOT reached
else {
count++;
}
lastChar = s.charAt(i);
}
String compressedString = c.toString();
return compressedString;
}
public static void main(String[] args) {
String longString = "aabccddvvv";
System.out.println(findCompressedString(longString, longString.length()));
}
}