Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Реализовать RLE-сжатие: AAAB -> A3B, BCCDDDAXXXX -> BC2D3AX4
- * @param {string} value
- * @return {string}
- */
- function rle(value) {
- if (!value) return '';
- if (typeof value !== 'string') throw 'Give me your ̶m̶o̶n̶e̶y string!';
- let res = value[0];
- let currCharCount = 1;
- for (var i = 1; i < value.length; i++) {
- if (res.endsWith(value[i])) {
- currCharCount += 1;
- } else {
- if (currCharCount !== 1) {
- res += currCharCount.toString();
- }
- res += value[i];
- currCharCount = 1;
- }
- }
- if (currCharCount !== 1) {
- res += currCharCount.toString();
- }
- return res;
- }
- console.log(rle('AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD'));
- console.log(rle('11333355555577777777'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement