-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCompression.java
More file actions
executable file
·59 lines (53 loc) · 1.74 KB
/
StringCompression.java
File metadata and controls
executable file
·59 lines (53 loc) · 1.74 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Neel_Kapadia
*/
public class StringCompression {
public int compress(char[] chars) {
int num = 1, ctr = 0;
char x;
char[] temp;
for (int i = 1; i < chars.length; i++) {
x = chars[i - 1];
if (chars[i] == chars[i - 1]) {
num++;
} else {
chars[ctr] = x;
ctr++;
temp = ("" + num).toCharArray();
if (temp.length != 1 || temp[0] != '1') {
for (int j = 0; j < temp.length; j++) {
chars[ctr] = temp[j];
System.out.print(chars[ctr]+" ");
ctr++;
}
}
num = 1;
}
}
chars[ctr] = chars[chars.length - 1];
ctr++;
temp = ("" + num).toCharArray();
if (temp.length != 1 || temp[0] != '1') {
for (int j = 0; j < temp.length; j++) {
chars[ctr] = temp[j];
ctr++;
}
}
// for (int i = 0; i < ctr; i++) {
// System.out.print(chars[i]);
// }
return ctr;
}
public static void main(String[] args) {
StringCompression sc = new StringCompression();
// char arr[] = {'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'a', 'b'};
char arr[] = {'a', 'a','a', 'a','a', 'a','a', 'a'};
System.out.println(sc.compress(arr));
}
}