-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q4.java
33 lines (32 loc) · 932 Bytes
/
Q4.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
public class Q4 {
public static int compress(char[] chars) {
//write your code here
int ans=0;
int i=0;
while(i<chars.length) {
char ch=chars[i];
int count =0;
while(i<chars.length && chars[i]==ch) {
i++;
count++;
}
chars[ans++]=ch;
if(count>1) {
String countSring=String.valueOf(count);
for(char c:countSring.toCharArray()) {
chars[ans++]=c;
}
}
}
return ans;
}
public static void main(String[] args) {
char[] chars = {'a', 'a', 'b', 'b', 'c', 'c', 'c'};
int newSize = compress(chars);
System.out.println("New length: " + newSize);
for (int i = 0; i < newSize; i++) {
System.out.print(chars[i] + " ");
}
System.out.println();
}
}