Batisk_AFF

rle-js-rubaxa

Dec 5th, 2021 (edited)
871
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Реализовать RLE-сжатие: AAAB -> A3B, BCCDDDAXXXX -> BC2D3AX4
  3.  * @param  {string} value
  4.  * @return {string}
  5.  */
  6. function rle(value) {
  7.     if (!value) return '';
  8.     if (typeof value !== 'string') throw 'Give me your ̶m̶o̶n̶e̶y string!';
  9.    
  10.     let res = value[0];
  11.     let currCharCount = 1;
  12.    
  13.     for (var i = 1; i < value.length; i++) {
  14.         if (res.endsWith(value[i])) {
  15.             currCharCount += 1;
  16.         } else {
  17.             if (currCharCount !== 1) {
  18.                 res += currCharCount.toString();
  19.             }
  20.             res += value[i];
  21.             currCharCount = 1;
  22.         }
  23.     }
  24.    
  25.     if (currCharCount !== 1) {
  26.         res += currCharCount.toString();
  27.     }
  28.    
  29.     return res;
  30. }
  31.  
  32.  
  33. console.log(rle('AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD'));
  34. console.log(rle('11333355555577777777'));
RAW Paste Data