Skip to content

Instantly share code, notes, and snippets.

@0001vrn
Created July 1, 2017 03:41
Show Gist options
  • Select an option

  • Save 0001vrn/d848741909de5105ecf8a04a760a6162 to your computer and use it in GitHub Desktop.

Select an option

Save 0001vrn/d848741909de5105ecf8a04a760a6162 to your computer and use it in GitHub Desktop.
Run Length Coding
import java.util.*;
import java.lang.*;
import java.io.*;
class RunLengthCoding
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
String s = "aaabbbccc";
LinkedHashMap<Character,Integer> hm=new LinkedHashMap<>();
for(int i=0;i<s.length();i++)
{
hm.compute(s.charAt(i), (key, val) -> {
if (val == null) {
return 1;
} else {
return val + 1;
}
});
}
for (Map.Entry<Character, Integer> entry : hm.entrySet()) {
System.out.print(entry.getKey()+""+entry.getValue());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment