nuxy / leetcode-443

Benchmark created on


Description

String Compression

Setup

const compress = function(chars) {
  const len = chars.length;
  const arr = [];

  let count = 0;

  for (let i = 0; i < len; i++) {
    const currChar = chars[i];
    const prevChar = chars[i - 1];
    const nextChar = chars[i + 1];

    count++;

    if (prevChar !== currChar) {
      arr.push(currChar);
    }

    if (currChar !== nextChar && len > 1) {
      arr.push(count);
      count = 0;
    }
  }

  return arr.length;
};

Test runner

Ready to run.

Testing in
TestOps/sec
Test 1
compress(['a','a','b','b','c','c','c']); // 6
ready
Test 2
compress(['a']); // 1
ready
Test 3
compress(['a','b','b','b','b','b','b','b','b','b','b','b','b']); // 4
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.