Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Length of String Compression (Main Thread)

Here is the interview question prompt, presented for reference.

Let's define the "compacting" of a string as taking an input like the following: 'sstttrrrr'.

We want to take the number of sequential appearances (number of times it shows up in a row) of each letter:

s: 2
t: 3
r: 4

And denote them next to the original character in the string, getting rid of the duplicates. In our above example, our output is: 's2t3r4'.

How long is this compacted string? Could you write a method that takes in a string and returns the length of the compacted one? Could you solve it using only O(1) extra space?

Here are few other examples:

compactLength('aabbbbbbbbbbbbb')
// 5 because we end up with `a2b13`

Another to consider:

compactLength('s')
// 1 because we still end up with just `s`

Constraints

  • Length of the given string <= 100000
  • The string comprises of ASCII characters
  • Expected time complexity : O(n)
  • Expected space complexity : O(1)

You can see the full challenge with visuals at this link.

Challenges • Asked almost 7 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Nov 30, 2017:

This is the main discussion thread generated for Length of String Compression.