-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDPP29.java
More file actions
58 lines (50 loc) · 1.47 KB
/
DPP29.java
File metadata and controls
58 lines (50 loc) · 1.47 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
/*
Good morning! Here's your coding interview problem for today.
This problem was asked by Amazon.
Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and character. For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A".
Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely of alphabetic characters. You can assume the string to be decoded is valid.
*/
import java.io.*;
import java.util.*;
class DPP29
{
public static void main(String[] ar)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
System.out.println(decoding(encoding(s)));
}
public static String encoding(String str)
{
int len = str.length(),i=0,j=0,c=0;
String e="";
while(i<len && j<len)
{
if(str.charAt(i)==str.charAt(j))
{
c++;
j++;
}
else
{
e+=Integer.toString(c)+Character.toString(str.charAt(i));
c=0;
i=j;
}
}
if(c!=0)
e+=Integer.toString(c)+Character.toString(str.charAt(i));
return e;
}
public static String decoding(String str)
{
int len = str.length();
String d = "";
for (int i=0;i<len;i+=2)
{
int j=i+1;
for(int k=0;k<Character.getNumericValue(str.charAt(i));k++)
d+=Character.toString(str.charAt(j));
}
return d;
}
}