Skip to content

Commit dba7523

Browse files
author
sushant
committed
run length encoding
1 parent d60509f commit dba7523

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed

.classpath

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
5+
<attributes>
6+
<attribute name="owner.project.facets" value="java"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="output" path="build/classes"/>
10+
</classpath>

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build/

.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>HackerRank</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
21+
<nature>org.eclipse.jdt.core.javanature</nature>
22+
</natures>
23+
</projectDescription>

.settings/org.eclipse.jdt.core.prefs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.compliance=1.8
5+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
6+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
7+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<faceted-project>
3+
<installed facet="java" version="1.8"/>
4+
</faceted-project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.sushantjhingan;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Scanner;
8+
9+
10+
/**
11+
* Run-length encoding (RLE) is a very simple form of data compression in which runs of data
12+
* (that is, sequences in which the same data value occurs in many consecutive data elements)
13+
* are stored as a single data value and count, rather than as the original run. (Wikipedia)
14+
*
15+
* @author Sushant Jhingan
16+
* Complexity O(n)
17+
*/
18+
public class RunLengthEncoding {
19+
20+
private static String method1(String input) {
21+
StringBuilder output = new StringBuilder();
22+
23+
for (int i = 0, count = 1; i < input.length(); i++) {
24+
if (i + 1 < input.length() && input.charAt(i) == input.charAt(i + 1))
25+
count++;
26+
else {
27+
output = output.append(Integer.toString(count))
28+
.append(Character.toString(input.charAt(i)));
29+
count = 1;
30+
}
31+
}
32+
return output.toString();
33+
}
34+
35+
36+
private static String method2(String source) {
37+
StringBuffer dest = new StringBuffer();
38+
for (int i = 0; i < source.length(); i++) {
39+
int runLength = 1;
40+
while(i+1 < source.length() && source.charAt(i) == (source.charAt(i+1)))
41+
{
42+
runLength++;
43+
i++;
44+
}
45+
46+
dest.append(source.charAt(i));
47+
dest.append(runLength);
48+
}
49+
return dest.toString();
50+
}
51+
private static String method3(String input) {
52+
53+
if(input.equals("") || input.trim().equals("")){
54+
return "";
55+
}
56+
57+
Map<Character, Integer> map = new HashMap<Character, Integer>();
58+
List<Character> list = new ArrayList<Character>();
59+
for(int i=0;i<input.length();i++){
60+
if(map.get(input.charAt(i)) != null){
61+
int count = map.get(input.charAt(i)) + 1;
62+
63+
map.put(input.charAt(i), count);
64+
65+
}else{
66+
map.put(input.charAt(i), 1);
67+
list.add(input.charAt(i));
68+
}
69+
}
70+
StringBuilder builder = new StringBuilder();
71+
for(Character s: list){
72+
builder.append(map.get(s));
73+
builder.append(s);
74+
}
75+
76+
return builder.toString();
77+
}
78+
79+
80+
public static void main(String[] args) {
81+
Scanner scan = new Scanner(System.in);
82+
String s = scan.nextLine();
83+
System.out.println(method1(s));
84+
85+
}
86+
}

0 commit comments

Comments
 (0)