stillearning
stillearning

Reputation: 403

How to compress a string in-place

I have been working on this problem on leetcode https://leetcode.com/problems/string-compression/


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.


I almost have a solution, but I can't seem to count the last character in the string and I also am not sure how to make it so if there is only an amount of one of a character that I do not show 1 in the array.

I feel like I'm pretty close and I'd like to try and keep the solution that I have without altering it too much if possible.

This is what I have so far. chars is a list of characters

def compress(chars):
    char = 0
    curr = 0
    count = 0
    while curr < len(chars):
        if chars[char] == chars[curr]:
            count += 1
        else:
            # if count == 1:
            #     break
            # else:
            chars[char-1] = count
            char = curr
            count = 0
        curr += 1
    chars[char-1] += 1
    return chars


print(compress(["a", "a", "b", "b", "c", "c", "c"]))

Upvotes: 1

Views: 1297

Answers (1)

zedfoxus
zedfoxus

Reputation: 37079

I wasn't quite able to format your code to get the answer you were seeking. Based on your answer, I was able to put together code and explanation that could help you:

def compress(chars):

    count = 1
    current_position = 0

    # if it's a single character, just return a 
    # a basic array with count
    if len(chars) == 1:
        chars.append("1")
        return chars

    # loop till the 2nd last character is analyzed
    while current_position < len(chars) - 1:

        # assume that we haven't reached the 2nd last character
        # if next character is the same as the current one, delete
        # the current one and increase our count
        while current_position < len(chars) - 1 and \
                chars[current_position] == chars[current_position + 1]:
            del chars[current_position]
            count += 1

        # if next character isn't the same, time to add the count to
        # the list. Split the number into 
        # character list (e.g. 12 will become ["1", "2"]
        # insert those numbers behind the character and increment position
        for x in str(count):
            chars.insert(current_position + 1, str(x))
            current_position += 1

        # great, on to the next character
        current_position += 1

        # if we are now at the last character, it's a lonely character
        # give it a counter of 1 and exit the looping
        if current_position == len(chars) - 1:
            chars.append("1")
            break

        count = 1

    return chars

mylist = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
print(compress(mylist))

Results

mylist = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
['a', '1', 'b', '1', '2']

mylist = ["a","a","a","a","a","a","a","a","a","a","b","b","b","b","b","b","b","b","b","b","b","b"]
['a', '1', '0', 'b', '1', '2']

mylist = ["a"]
['a', '1']

mylist = ["a","b"]
['a', '1', 'b', '1']

mylist = ["a","a","b","b","c","c","c"]
['a', '2', 'b', '2', 'c', '3']

Upvotes: 1

Related Questions