In-place Run-Length String Compressions using C++

  • 时间:2020-09-25 11:32:47
  • 分类:网络文摘
  • 阅读:95 次

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.

Could you solve it using only O(1) extra space?

Example 1:

Input:
[“a”,”a”,”b”,”b”,”c”,”c”,”c”]

Output:
Return 6, and the first 6 characters of the input array should be: [“a”,”2″,”b”,”2″,”c”,”3″]

Explanation:
“aa” is replaced by “a2”. “bb” is replaced by “b2”. “ccc” is replaced by “c3”.

Example 2:

Input:
[“a”]

Output:
Return 1, and the first 1 characters of the input array should be: [“a”]

Explanation:
Nothing is replaced.

Example 3:

Input:
[“a”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”]

Output:
Return 4, and the first 4 characters of the input array should be: [“a”,”b”,”1″,”2″].

Explanation:
Since the character “a” does not repeat, it is not compressed. “bbbbbbbbbbbb” is replaced by “b12”.
Notice each digit has it’s own entry in the array.

Note:
All characters have an ASCII value in [35, 126].
1 <= len(chars) <= 1000.

How to Compress strings in-place using Run-length Encoding?

Edges cases have to be considered carefully when the input strings are length zero or length one. In both cases, no modifications take place and we should immediately return the length of the strings.

We have two index pointers, one for scanning the original string e.g. i, and the other for modifying in-place e.g. j, and we know that the j is not any faster than i as the compressed string should not be longer than the original string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
    int compress(vector<char>& chars) {
        int n = chars.size();
        if (n <= 1) return n;
        int j = 0, i = 0;
        while (i < n) {
            int k = i;
            // skip duplicates
            while ((k + 1 < n) && chars[k] == chars[k + 1]) {
                k ++;
            }
            // compute the repeat count
            int count = k - i + 1;
            // fill the character first
            chars[j ++] = chars[i];
            // skip writting 1 as counter
            if (count == 1) {
                i ++;
                continue;
            }
            // write out the reversed 'counter'
            int start = j;
            while (count > 0) {
                chars[j ++] = (char)(48 + (count % 10));
                count /= 10;
            }
            // reverse the counter e.g. if (count >= 10)
            std::reverse(begin(chars) + start, begin(chars) + j);
            // navigate to next character
            i = k + 1; 
        }
        return j;
    }
};
class Solution {
public:
    int compress(vector<char>& chars) {
        int n = chars.size();
        if (n <= 1) return n;
        int j = 0, i = 0;
        while (i < n) {
            int k = i;
            // skip duplicates
            while ((k + 1 < n) && chars[k] == chars[k + 1]) {
                k ++;
            }
            // compute the repeat count
            int count = k - i + 1;
            // fill the character first
            chars[j ++] = chars[i];
            // skip writting 1 as counter
            if (count == 1) {
                i ++;
                continue;
            }
            // write out the reversed 'counter'
            int start = j;
            while (count > 0) {
                chars[j ++] = (char)(48 + (count % 10));
                count /= 10;
            }
            // reverse the counter e.g. if (count >= 10)
            std::reverse(begin(chars) + start, begin(chars) + j);
            // navigate to next character
            i = k + 1; 
        }
        return j;
    }
};

We write out the counter in the reverse order, and then use the std::reverse to reverse the counter part. The above C++ run-length encoding compression takes in-place and runs at O(1) space and O(N) time complexity.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
北京体育频道直播-北京体育在线直播观看「高清」  五星体育直播-五星体育在线直播观看「高清」  风云足球直播-风云足球在线直播观看「高清」  吉林篮球直播-吉林篮球在线直播观看「高清」  江苏体育直播-江苏体育在线直播观看「高清」  广州竞赛直播-广州竞赛频道在线直播观看「高清」  广东体育频道直播-广东体育频道在线直播观看「高清」  高尔夫网球频道直播-CCTV高尔夫网球在线直播「高清」  央视台球频道直播-斯诺克直播「高清」  山东体育频道直播-山东体育在线直播观看「高清」 
评论列表
添加评论