fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String args[]) {
  11.  
  12. String str = "asbbaamkkkjkssg";
  13.  
  14. String value = runLengthEncoding(str);
  15. System.out.println(value);
  16. }
  17.  
  18. public static String runLengthEncoding(String text) {
  19. String encodedString = "";
  20.  
  21. for (int i = 0, count = 1; i < text.length(); i++) {
  22. if (i + 1 < text.length() && text.charAt(i) == text.charAt(i + 1))
  23. count++;
  24. else {
  25. encodedString = encodedString.concat(Character.toString(text.charAt(i)));
  26. if(count>1) {
  27. encodedString = encodedString.concat(Integer.toString(count));
  28. }
  29. count = 1;
  30. }
  31. }
  32. return encodedString;
  33. }
  34. }
Success #stdin #stdout 0.06s 2184192KB
stdin
Standard input is empty
stdout
asb2a2mk3jks2g