-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunLengthEncoding.java
More file actions
51 lines (42 loc) · 908 Bytes
/
RunLengthEncoding.java
File metadata and controls
51 lines (42 loc) · 908 Bytes
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
import java.util.Scanner;
/*
Sample input: 0111001
Output: 0(1,3)(0,2)1
*/
public class A1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "";
int count = 1;
int len = 0;
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
s = s + ' ';
len = s.length();
for(int i = 0 ; i <len-1 ; i++ ) {
if( s.charAt(i) == ' ') {
break;
}
if(s.charAt(i) != s.charAt(i+1)) {
System.out.print(s.charAt(i));
}
if(s.charAt(i) == s.charAt(i+1)) {
char in = s.charAt(i);
for(int j = i+1 ; j <len ; j++ ) {
if(in == s.charAt(j)) {
count++;
}
if(in != s.charAt(j)) {
break;
}
}
}
if (count >1) {
System.out.print("("+s.charAt(i)+",");
System.out.print(count+")");
i = i + count-1;
count =1;
}
}
}
}