简体   繁体   中英

String Compression

A list of strings should be compressed into their shortest forms by expressing repetitions with numbers. If a letter only shows up only once, the "1" before it should be omitted.

For example

"aabbaccc" can be compressed into "2a2ba3c" if subdivided into units of 1.

"ababcdcdababcdcd" can be compressed into "2ab2cd2ab2cd" if subdivided into units of 2.

"abcabcdede" can be compressed into "2abcdede" if subdivided into units of 3 and so on.

I need to compress the following strings to the following shortest lengths

  1. "aabbaccc" , 7
  2. "ababcdcdababcdcd" , 9
  3. "abcabcdede" , 8
  4. "abcabcabcabcdededededede" , 14
  5. "xababcdcdababcdcd" , 17

My code only works for case 1 and 5 because I only know how to subdivide strings into units of 1. I am not sure how to subdivide for units greater than 1 and need someone to edit my code.

// traverse string, keep count of repeated chars
// if cur and next char is the same, inc count
// otherwise, concat cur char and count to output string, reset counter to 1
// return compressed string, only if the length is less than the original string, otherwise, return original string

let stringCompression = (s) => {
  let out = '';
  let count = 1;

  for (let i = 0; i < s.length; i++) {
    let cur = s[i];
    let next = s[i + 1];

    if (cur === next) {
      count++;
    } else {
      out += String(count) + cur;
      final = out.replace('1','')
      count = 1;
    }
  }
  return final.length < s.length ? final : s;
}
console.log(stringCompression('aabbaccc').length); // is 7

For anyone who can read Korean, the original source is question 1 from here

Let's call compression parts a chunk. For example, in "ababcdcdababcdcd" (which can be compressed either to "2ab2cd2ab2cd" or to "2ababcdcd") "ab", "cd" and "abcd" are chunks. You can write a cycle:

for(let i = N; i > ; i--)

where N is the maximum possible length of a chunk (obviously it is the length of a string divided by 2) and i is current chunk size we assume. So we will start looking for the biggest possible chunks. In case of "xababcdcdababcdcd" it is:

  1. "xababcdc"
  2. "ababcdcd"
  3. ... And each time you find a repeating chunk, you replace it

However, there is another path. I think it may be possible to solve this task by using Recursive Regular Expressions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM