[CareerCup] 1.5 Compress String 压缩字符串

1.5 Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string.

这道题让我们压缩给定的字符串,压缩方法是对于重复的字符,用数字来表示重复的个数,这种压缩方法对于有很多重复字符具有很高的压缩效率,但是对于不重复的字符串,压缩后的表示方法反而比不压缩占空间大。所以我们首先要先来计算下压缩后的字符串的长度,和原字符串长度比较,如果大的话,则直接返回原字符串,如果小的话,则我们就开始压缩。那么我们需要建立一个新字符串来保存压缩后的字符串,这里书中特别提到了用字符串的相加的方法是很没有效率的,下面英文部分摘自Cracking the coding interview 5th edition的第72页:

Imagine you were concatenating a list of strings, as shown below. What would the running time of this code be? For simplicity, assume that the strings are all the same length (call this x) and that there are n strings.

public String joinWords(String[] words) {
    String sentence = "";
    for (String w : words) {
        sentence = sentence + w;
    }
    return sentence;
}

On each concatenation, a new copy of the string is created, and the two strings are copied over, character by character. The first iteration requires us to copy x characters. The second iteration requires copying 2x characters.The third iteration requires 3x, and
so on.The total time therefore is 0(x + 2x + ... + nx). This reduces to 0(xn2). (Why isn‘t it 0(xnn)? Because 1 + 2 + ... + nequals n(n+l)/2,orO(n2).)

根据上面所说,字符串的拼接余姚拷贝拼接的两个字符串,当字符串长度很长的时候,这种方法非常没有效率,所以我们要避免使用拼接的方法。那么我们的替代方法是先声明好一个定长的字符串,然后给每个位置赋值。压缩后的字符串长度我们开始计算过了,所以只需要给每个位置赋值即可,跟之前那道1.4 Replace Spaces 替换空格有些相似,参见代码如下:

class Solution {
public:
    string compress(string s) {
        int newLen = countCompression(s);
        if (newLen >= s.size()) return s;
        cout << newLen << endl;
        string res(newLen, ‘ ‘);
        char c = s[0];
        int cnt = 1, idx = 0;
        for (int i = 1; i < s.size(); ++i) {
            if (s[i] == c) ++cnt;
            else {
                res[idx++] = c;
                for (auto a : to_string(cnt)) res[idx++] = a;
                c = s[i];
                cnt = 1;
            }
        }
        res[idx++] = c;
        for (auto a : to_string(cnt)) res[idx++] = a;
        return res;
    }
    int countCompression(string s) {
        if (s.empty()) return 0;
        int res = 0, cnt = 1;
        char c = s[0];
        for (int i = 1; i < s.size(); ++i) {
            if (s[i] == c) ++cnt;
            else {
                c = s[i];
                res += 1 + to_string(cnt).size();
                cnt = 1;
            }
        }
        res += 1 + to_string(cnt).size();
        return res;
    }
};
时间: 2024-08-09 10:27:10

[CareerCup] 1.5 Compress String 压缩字符串的相关文章

[CareerCup] 1.2 Reverse String 翻转字符串

1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string. 这道题让我们用C++或C语言来翻转一个字符串,不算一道难题,在之前那道Reverse Words in a String 翻转字符串中的单词中用到了这个函数,跟那道题比起来,这题算简单的了.C语言的版本要比C++的稍微复杂一些,应为string类集成了很多有用的功能,比如得到字符串的长度,用下标

[CareerCup] 18.8 Search String 搜索字符串

18.8 Given a string s and an array of smaller strings T, design a method to search s for each small string in T. class SuffixTreeNode { public: unordered_map<char, SuffixTreeNode*> children; char value; vector<int> indexes; void insertString(s

[LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器

Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext. The given compressed string will be in the form of each letter followed by a positive integer representing the numbe

String压缩 解压缩

数据传输时,有时需要将数据压缩和解压缩,本例使用GZIPOutputStream/GZIPInputStream实现. 1.使用ISO-8859-1作为中介编码,可以保证准确还原数据 2.字符编码确定时,可以在decompress方法最后一句中显式指定编码 package com.bcxin.business.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.

NYOJ 1067 Compress String(区间dp)

Compress String 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描述 One day,a beautiful girl ask LYH to help her complete a complicated task-using a new compression method similar to Run Length Encoding(RLE) compress a string.Though the task is difficult, LYH is

图片转成二进制并且压缩字符串

今天工作需要写了一个把图片转成字符串,并且压缩的小功能,其中用到了多线程去转换字符串,感觉写的还是不错的,拿出来分享一下 1 /// <summary> 2 /// 创建多核任务 3 /// </summary> 4 /// <param name="bytimages"></param> 5 /// <param name="count">创建几个核</param> 6 public stri

华为上机练习题--压缩字符串

题目: 通过键盘输入一串小写字母(a~z)组成的字符串.请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串. 压缩规则: 1.仅压缩连续重复出现的字符.比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc". 2.压缩字段的格式为"字符重复的次数+字符".例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz". 要求实现函数: void str

noi Big String 超级字符串

//来自2017青岛信息竞赛第一题 9269:Big String超级字符串 查看 提交 统计 提问 总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  131072kB 描述 fish在无数次oi竞赛的狱炼之后,悟出一个真理,往往越容易的题目,陷阱越深.由此,fish创作了这道题目. fish首先给出两个短串A='4567' (4个字符), B='123'(3个字符).反复进行如下的操作得到一个长串C. (1)C=B+A (例如:A='4567' ,B='123

从String类型字符串的比较到StringBuffer和StringBuilder

1. String类型 String类源码 为了从本质上理解String类型的特性所在,我们从String类型的源码看起,在源码中String类的注释中存在以下: /**Strings are constant; their values cannot be changed after they * are created. String buffers support mutable strings. * Because String objects are immutable they ca