You are given a string with lower case letters only. Compress it by putting the count of the letter after it. If the letter appears once,

Example:

compress function:

input = aaaabbc output = a4b2c

decompress function:

input = a2bc3 output = aabccc

 public class Solution {
public String compress(String input) {
int startIndex = ;
StringBuilder output = new StringBuilder();
while (startIndex < input.length()) {
int nextIndex = findNextIndex(input, startIndex);
int length = nextIndex - startIndex;
output.append(input.charAt(startIndex));
if (length != ) {
output.append(length);
}
startIndex = nextIndex;
}
return output.toString();
} // a4b2c
public String decompress(String input) {
int startIndex = ;
StringBuilder output = new StringBuilder();
while (startIndex < input.length()) {
char ch = input.charAt(startIndex);
int numberEndIndex = findNumberEndIndex(input, startIndex);
if (numberEndIndex - startIndex == ) {
repeatLetters(output, , ch);
} else {
int count = Integer.parseInt(input.substring(startIndex + , numberEndIndex));
repeatLetters(output, count, ch);
}
startIndex = numberEndIndex;
}
return output.toString();
} private int findNumberEndIndex(String input, int startIndex) {
int endIndex = startIndex + ;
while (endIndex < input.length() && isNumber(input.charAt(endIndex))) {
endIndex++;
}
return endIndex;
} private boolean isNumber(char ch) {
return ch >= '' && ch <= '';
} private void repeatLetters(StringBuilder output, int count, char ch) {
while (count >= ) {
output.append(ch);
count--;
}
} private int findNextIndex(String input, int startIndex) {
int endIndex = startIndex + ;
while (endIndex < input.length() && input.charAt(endIndex) == input.charAt(startIndex)) {
endIndex++;
}
return endIndex;
}
}

follow-up

如果字符串里有数字,比如44443333aaabbc, 它会被转成 /44/34a3b2c

其实对于这种情况的处理还是比较简单的,我们只需要知道当前letter是否是数字,如果是数字,我们就在前面加‘/’。对于decompress, 我们也需要判断当前是否是‘/’,如果是,表明第一个是数字,数字后面的到下一个‘/’之前都是个数。

 public class Solution {
public String compress(String input) {
int startIndex = ;
StringBuilder output = new StringBuilder();
while (startIndex < input.length()) {
int nextIndex = findNextIndex(input, startIndex);
int length = nextIndex - startIndex;
char letter = input.charAt(startIndex);
if (isNumber(letter)) {
output.append('/');
}
output.append(letter);
if (length != ) {
output.append(length);
}
startIndex = nextIndex;
}
return output.toString();
} // /44/35b11a4c
// b11a4c/44/35
public String decompress(String input) {
int startIndex = ;
StringBuilder output = new StringBuilder();
while (startIndex < input.length()) {
char ch = input.charAt(startIndex);
if (ch == '/') {
startIndex++;
ch = input.charAt(startIndex);
}
int numberEndIndex = findNumberEndIndex(input, startIndex);
if (numberEndIndex - startIndex == ) {
repeatLetters(output, , ch);
} else {
int count = Integer.parseInt(input.substring(startIndex + , numberEndIndex));
repeatLetters(output, count, ch);
}
startIndex = numberEndIndex;
}
return output.toString();
} private int findNumberEndIndex(String input, int startIndex) {
int endIndex = startIndex + ;
while (endIndex < input.length() && isNumber(input.charAt(endIndex))) {
endIndex++;
}
return endIndex;
} private boolean isNumber(char ch) {
return ch >= '' && ch <= '';
} private void repeatLetters(StringBuilder output, int count, char ch) {
while (count >= ) {
output.append(ch);
count--;
}
} private int findNextIndex(String input, int startIndex) {
int endIndex = startIndex + ;
while (endIndex < input.length() && input.charAt(endIndex) == input.charAt(startIndex)) {
endIndex++;
}
return endIndex;
}
}

Compress and decompress string的更多相关文章

  1. use zlib lib to compress or decompress file

    If you want to compress or decompress file when writing C++ code,you can choose zlib library,that's ...

  2. [Algo] 175. Decompress String II

    Given a string in compressed form, decompress it to the original string. The adjacent repeated chara ...

  3. [转][C#]压缩解压缩类 GZipStream

    本文来自:https://msdn.microsoft.com/zh-cn/library/system.io.compression.gzipstream(v=vs.100).aspx using ...

  4. 基本的文件I/O

    基本的文件 I/O 抽象基类 Stream 支持读取和写入字节. Stream 集成了异步支持. 其默认实现根据其相应的异步方法来定义同步读取和写入,反之亦然. 所有表示流的类都是从 Stream 类 ...

  5. String压缩 解压缩

    数据传输时,有时需要将数据压缩和解压缩,本例使用GZIPOutputStream/GZIPInputStream实现. 1.使用ISO-8859-1作为中介编码,可以保证准确还原数据 2.字符编码确定 ...

  6. 解压与压缩(把dataset转为string、、 )

    /// <summary> /// 压缩 解压 /// </summary> public class ZipHelper { #region 压缩解缩 /// <sum ...

  7. c#文件操作

    1.创建文件夹 //using System.IO; Directory.CreateDirectory(%%1);   2.创建文件 //using System.IO; File.Create(% ...

  8. gzip的使用

    经常会有文件过大,给文件的传输和增添了很多的麻烦,早先得知apach有个base64貌似可以用来压缩文件,但是测试没有什么效果,反而增大了文件的大小.今天了解了java自带的gzip包,如获至宝,超级 ...

  9. 中转Http请求

    应用场景:公司与外部公司数据对接,外部公司需申请指定IP访问.而本地ip经常变动,无法因ip变动时刻向外部公司申请绑定IP,给本地程序调试带来麻烦,故只能在指定ip服务器上搭建请求中转http请求: ...

随机推荐

  1. Educational Codeforces Round 34 (Rated for Div. 2) B题【打怪模拟】

    B. The Modcrab Vova is again playing some computer game, now an RPG. In the game Vova's character re ...

  2. learning armbian steps(8) ----- armbian 源码分析(三)

    在lib/main.sh当中 ) == main.sh ]]; then echo "Please use compile.sh to start the build process&quo ...

  3. 51 Nod 1572 宝岛地图

    1572 宝岛地图  题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 勇敢的水手们到达了一个小岛,在这个小岛上,曾 ...

  4. Pycharm下将py文件打包成exe文件

    1. 在PyCharm下安装PyInstaller 1. 首先,打开自己要发布的工程   2. 点击底部的[Terminal]打开终端,中输入命令pip install pyinstaller后回车, ...

  5. 2019.9.5 Balanced Lineup

    题目传送门 板子*3 #include<iostream> #include<cstdio> #include<cstring> #include<cmath ...

  6. elastic search&logstash&kibana 学习历程(四)kibana安装部署和使用

    kibana在linux上的部署安装 运行环境是centos7 基于jdk8 下载安装包:wget https://artifacts.elastic.co/downloads/kibana/kiba ...

  7. Django常见命令

    在Django的使用过程中需要使用命令让Django进行一些操作,例如创建Django项目,启动Django程序,创建新的APP,数据库迁移等. 1. 创建Django项目 新建一个文件夹来存放项目文 ...

  8. 【redis 学习系列08】Redis小功能大用处02 Pipeline、事务与Lua

    3.Pipeline 3.1 Pipeline概念 Redis客户端执行一条命令分为如下四个过程: (1)发送命令 (2)命令排队 (3)命令执行 (4)返回结果 其中(1)和(4)称为Round T ...

  9. Kettle环境的安装

    Kettle是绿色免安装的,下载完解压之后找到Spoon.bat,直接执行就好 欢迎界面 Kettle主要分为转换和作业2类 新建一个转换demo 创建了一个转换demo后,由于是数据抽取,因此我们抽 ...

  10. 用itext生成PDF报错:Font 'STSong-Light1' with 'UniGB-UCS2-H' is not recognized.

    用itext生成PDF报错,加上try catch捕获到异常是 BaseFont bFont = BaseFont.createFont("STSong-Light1", &quo ...