Compress and decompress string
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的更多相关文章
- 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 ...
- [Algo] 175. Decompress String II
Given a string in compressed form, decompress it to the original string. The adjacent repeated chara ...
- [转][C#]压缩解压缩类 GZipStream
本文来自:https://msdn.microsoft.com/zh-cn/library/system.io.compression.gzipstream(v=vs.100).aspx using ...
- 基本的文件I/O
基本的文件 I/O 抽象基类 Stream 支持读取和写入字节. Stream 集成了异步支持. 其默认实现根据其相应的异步方法来定义同步读取和写入,反之亦然. 所有表示流的类都是从 Stream 类 ...
- String压缩 解压缩
数据传输时,有时需要将数据压缩和解压缩,本例使用GZIPOutputStream/GZIPInputStream实现. 1.使用ISO-8859-1作为中介编码,可以保证准确还原数据 2.字符编码确定 ...
- 解压与压缩(把dataset转为string、、 )
/// <summary> /// 压缩 解压 /// </summary> public class ZipHelper { #region 压缩解缩 /// <sum ...
- c#文件操作
1.创建文件夹 //using System.IO; Directory.CreateDirectory(%%1); 2.创建文件 //using System.IO; File.Create(% ...
- gzip的使用
经常会有文件过大,给文件的传输和增添了很多的麻烦,早先得知apach有个base64貌似可以用来压缩文件,但是测试没有什么效果,反而增大了文件的大小.今天了解了java自带的gzip包,如获至宝,超级 ...
- 中转Http请求
应用场景:公司与外部公司数据对接,外部公司需申请指定IP访问.而本地ip经常变动,无法因ip变动时刻向外部公司申请绑定IP,给本地程序调试带来麻烦,故只能在指定ip服务器上搭建请求中转http请求: ...
随机推荐
- Luogu P4198 楼房重建 分块 or 线段树
思路:分块 提交:2次(第一次的求解有问题) 题解: 设块长为$T$,我们开$N/T$个单调栈,维护每一块的上升斜率. 修改时暴力重构整个块,$O(T)$ 求解时记录一个最大斜率$lst$,然后块内二 ...
- hdu 5187 zhx's contest (快速幂+快速乘)
zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 2019CCPC秦皇岛自我反省&部分题解
练了一年半了,第一次打CCPC,险些把队友坑了打铁,最后也是3题危险捡了块铜. 非常水的点双连通,我居然不相信自己去相信板子,唉,结果整来整去,本来半个小时能出的题,整到了3个小时,大失误呀,不然就可 ...
- 百度智能api接口汇总
一:自然语言处理 # -*- coding: utf-8 -*- # @Author : FELIX # @Date : 2018/5/18 9:47 # pip install baidu-aip ...
- [Linux]安装kali虚拟机后忘记root密码
1. 登录时,按e进入编辑模式 2. 编辑模式 修改 ro 修改为 rw 添加 init=/bin/bash 修改完按 F10 3. 选择recovery mode 回车 4.输入命令passwd 设 ...
- python 列表切片之负数的含义代码示例
a = list(range(10)) print(a[::]) #复制一个列表 print(a[::2]) #每隔2个取一次 print(a[::3]) #每隔3个取一次 print(a[::-1] ...
- JS基础_数组简介
内建对象 宿主对象 自定义对象 数组(Array) - 数组也是一个对象 - 它和我们普通的对象功能类似,也是用来存储一些值的 - 不同的是普通对象是使用字符串作为属性名的 数组是使用数字来作为索引来 ...
- Google 插件的使用
每次看英文网页或者文档的时候总会碰到不认识单词,就想能不能选中单词就可以显示翻译?于是就安装Google浏览器的翻译插件,总的来说,蛮繁琐的. 1.先安装谷歌访问助手 (1.)直接百度谷歌访问助手 ( ...
- bash脚本获取绝对路径的最后一个目录名称
比如绝对路径是/root/autoHls/streamID 因为脚本里面想直接用这个streamID来推流 下面是方法 #!/bin/bash dir="/root/autoHls" ...
- Ubuntu 安装 docker,并上传到dockerhub
一.安装Docker apt-get -y install docker.io 链接: ln -sf /usr/bin/docker.io /usr/local/bin/docker 检查docker ...