algorithm to compress a string

string compression algorithm c#
string compression javascript
string compression - leetcode python
string compression interview question
recursive string compression
array compression
compress string data
best string compression algorithm in java

I want to write a function to compress the characters of a string such as 'bbbab' would be converted to 'b3ab' and I'm getting stuck

it's only printing "b3"

here's the code so far

let string = 'bbbab';
let letters = string.split("");
let currentAlphabetLetter = letters[0];
let count = 0;
let newString = [];

string.split("").forEach(letter => {
    if (letter === currentAlphabetLetter) {
        count++;
    } else {
        if (count > 0) {
            newString.push(`${currentAlphabetLetter}${count}`);
        } else {
        newString.push(`${letter}`);
    }
    count = 0;
    currentAlphabetLetter = letter;
  }
})


console.log(newString);

When you start a new letter, you need to set count to 1, otherwise you're not counting the first occurrence of the character.

This isn't a problem at the very beginning of the string, because you process the first letter twice: you extract it with let currentAlphabetLetter = letters[0]; and then process it again on the first iteration of the forEach. To make the beginning of the string the same as other occurrences, you should iterate over the substring starting with the 2nd character.

And rather than pushing onto an array, you should append to a string.

When count is 1 you need to append currentAlphabetLetter, not letter.

let string = 'bbbab';
let letters = string.split("");
let currentAlphabetLetter = letters[0];
let count = 1;
let newString = "";

string.substr(1).split("").forEach(letter => {
  if (letter === currentAlphabetLetter) {
    count++;
  } else {
    if (count > 1) {
      newString += `${currentAlphabetLetter}${count}`;
    } else {
      newString += `${currentAlphabetLetter}`;
    }
    count = 1;
    currentAlphabetLetter = letter;
  }
});

// Process the last letter
if (count > 1) {
  newString += `${currentAlphabetLetter}${count}`;
} else {
  newString += `${currentAlphabetLetter}`;
}

console.log(newString);

An efficient compression algorithm for short text strings, Check out Smaz: Smaz is a simple compression library suitable for compressing very short strings. As far as I can tell, the most popular compression algorithm that allows standard C string-handling routines to be re-used to handle compressed text (i.e., carefully avoids putting any 0x00 bytes in the compressed string, except as the end-of-compressed-data marker) is simple byte-pair encoding, also called dual-tile encoding or DTE.

You may do as follows;

var str = 'bbbabccc',
    res = [...str].reduce((r,c,i) => (r[r.length-1][0] === c || !i) ? (r[r.length-1] += c, r)
                                                                    : r.concat(c), [""])
                  .reduce((r,s) => r + s[0] + (s.length-1 ? s.length : ""), "");
console.log(res);

A Simple String Compression Algorithm, An efficient algorithm for encode 7-bit characters to 6 or 5 bits to reduce the size of a string. Given a string how would you compress it? Example input is not in the form of aabbccdd but like abcdgehrk. i.e. all chars in the string are different.(Note:Run Length Encoding wont work ,as it was one of the solutions which I gave but he said string does not have any repetitive characters) I gave the following two solutions but he did not

Like this should work for you

let string = 'bbbab';
let newString = [];

if (string.length < 2) { console.log(string); }
else {
    let letters = string.split("");
    let prev = letters[0];
    let count = 1;
    for( var i = 1; i < letters.length; i++) {
      if (letters[i] !== prev) {
        count > 1 ? newString.push(`${prev}${count}`) : newString.push(`${prev}`);
        count = 1;
      } else {
        count++;
      }
      prev = letters[i];
    }
    /* last element push */
    count > 1 ? newString.push(`${prev}${count}`) : newString.push(`${prev}`);
    console.log(newString.join(""));
}

String Compression - Algorithms Practice, Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would  This method will reduce the size of a string considerably when the string is lengthy and the compression ratio is not affected by the content of the string. The Algorithm Encoding . Let’s assume that we have a string with 8 characters (example: - “abcdefgh”). If we put this on a byte array, we get a byte array with the size of 8.

String Compression, To save space on the small postcards, they devise a string compression algorithm: If a character, , occurs times in a row, then it will be represented by , where is  Given an array of characters, compress it in-place. The length after compression must always be smaller than or equal to the original array. Every element of the array should be a character (not int) of length 1. After you are done modifying the input array in-place, return the new length of the array. Follow up:

Lossless compression, Lossless compression is a class of data compression algorithms that allows the original data to While, in principle, any general-purpose lossless compression algorithm (general-purpose meaning that they can accept any bitstring) can be  To save space on the small postcards, they devise a string compression algorithm: If a character, , occurs times in a row, then it will be represented by , where is the value of . For example, if the substring is a sequence of ' a ' (" aaaa "), it will be represented as " a4 ".

shoco, shoco is a C library to compress and decompress short strings. like gzip begins to be dwarfed by the advantages of the much more efficient algorithms it uses. A series of mini-posts on various technical interview questions. This one comes from Cracking the Coding Interview prep book. Implement a method to perform basic string compression using the counts…

Comments
  • "and I'm getting stuck" You have to provide more information about your problem than that. How exactly are you stuck? Do you get an error? Does your code not produce the output you expect? Which output do you get? Which do you expect? What is the input? Please see How to Ask.
  • Where do you get stuck?
  • It's only printing or adding rather, b3 to the array
  • can you explain this better?
  • @totalnoob I hope it helps.