Guest User

Untitled

a guest
Jan 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. package dojo.kata;
  2.  
  3. public class RLE {
  4.  
  5. public static String encode(String chaine) {
  6.  
  7. if (chaine == null || chaine.isEmpty())
  8. return "";
  9. if(chaine.length()==1)return encodeUnit(chaine);
  10.  
  11. String prefix="";
  12. int i=0;
  13. do {
  14. prefix+=chaine.charAt(i);
  15. i++;
  16. } while (i<chaine.length()&& chaine.charAt(i-1)==chaine.charAt(i));
  17. if(i<chaine.length()&&chaine.charAt(i-1)!=chaine.charAt(i)){
  18. return encodeUnit(prefix)+encode(chaine.substring(prefix.length()));
  19. }
  20.  
  21. return encodeUnit(chaine);
  22. }
  23.  
  24. private static String encodeUnit(String chaine) {
  25. return "" + chaine.length() + chaine.charAt(0);
  26. }
  27.  
  28. }
Add Comment
Please, Sign In to add comment