mramine364

RLE encode & decode

Jun 17th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function rle_encode(str){
  3.     let i=0, res="";
  4.     while( i<str.length ){
  5.         let oc=1;
  6.         while( i<str.length-1 && str[i]==str[i+1] ){
  7.             i++; oc++;
  8.         }
  9.         res += oc+str[i];
  10.         i++;
  11.     }
  12.     return res;
  13. }
  14.  
  15. function rle_decode(str){
  16.     let i=0, res="";
  17.     while( i<str.length ){
  18.         let nbr=0, k=0;
  19.         while( !isNaN(str[i]) ){
  20.             nbr = 10*nbr+parseInt(str[i]);
  21.             i++;
  22.         }
  23.         while( k<nbr ){
  24.             res += str[i];
  25.             k++;
  26.         }
  27.         i++;
  28.     }
  29.     return res;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment