File tree Expand file tree Collapse file tree 1 file changed +31
-16
lines changed Expand file tree Collapse file tree 1 file changed +31
-16
lines changed Original file line number Diff line number Diff line change 1
-
2
- n = len(chars)
3
- cnt = 1;
4
- i = 0;
5
- for j in range(1, len(chars)+1):
6
- if j < n and chars[j-1] == chars[j]:
7
- cnt += 1;
8
- else:
9
- chars[i] = chars[j-1];
10
- i += 1;
11
- if cnt > 1:
12
- for m in str(cnt):
13
- chars[i] = m;
14
- i += 1;
15
- cnt = 1;
16
- return i;
1
+ public int compress(char[ ] chars) {
2
+ char data = chars[ 0] ;
3
+ int index = 0;
4
+ int length = -1;
5
+ if(chars.length == 1){
6
+ return 1;
7
+ }else {
8
+ for (int i = 1;i< chars.length;i++){
9
+ if(chars[ i] !=data || i == chars.length-1){
10
+ chars[ ++length] =data;
11
+ if(i == chars.length -1 && chars[ i] ==data){
12
+ i++;
13
+ }
14
+ if(i-index>1){
15
+ char[ ] num = String.valueOf((i-index)).toCharArray();
16
+ for (int j=0;j<num.length;j++){
17
+ chars[ ++length] = num[ j] ;
18
+ }
19
+ }
20
+ if(i < chars.length - 1){
21
+ data = chars[ i] ;
22
+ index = i;
23
+ }
24
+ if(i == chars.length -1 && chars[ i] !=data){
25
+ chars[ ++length] =chars[ i] ;
26
+ }
27
+ }
28
+ }
29
+ }
30
+ return length + 1;
31
+ }
You can’t perform that action at this time.
0 commit comments