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请求: ...
随机推荐
- Chef and Problems(from Code-Chef FNCS) ( 回 滚 )
题目: 题意:给定序列,求[l,r]区间内数字相同的数的最远距离. 链接:https://www.codechef.com/problems/QCHEF #include<bits/stdc++ ...
- [Cogs] 最大数maxnumber
http://cogs.pro:8080/cogs/problem/problem.php?pid=1844 Luogu 的数据真zhizhang Cogs AC #include <iostr ...
- HZOJ 20190819 NOIP模拟26题解
考试过程: 照例开题,然后觉得三道题都挺难,比昨天难多了(flag×1),T1 dp?T2 数据结构? T3 dp?事实证明我是sb然后决定先搞T2,但是,woc,这题在说什么啊,我怎么看不懂题啊,连 ...
- JVM GC之垃圾收集器
简述 如果说收集算法时内存回收的方法论,那么垃圾收集器就是内存回收的具体实现.这里我们讨论的垃圾收集器是基于JKD1.7之后的Hotspot虚拟机,这个虚拟机包含的所有收集器如图: Serial 收集 ...
- 使用Xshell链接阿里云服务
1.下载Xshell,进入xshell官网 https://xshell.en.softonic.com/,选择免费版本进行下载,在该页面https://www.netsarang.com/zh/fr ...
- python排序之冒泡排序
def sort(list): for i in range(len(list)): for j in range(len(list) - i - 1): if list[j] < list[j ...
- phpstorm clone 码云项目到本地 Version Control 不显示
最近在用码云作为代码仓库,但是建了仓库,也填加了 SSH,把项目利用 phpstorm VCS --> checkout from version control --> git 克隆到 ...
- flutter 安卓再次点击返回退出应用
安卓手机点击实体或者虚拟返回键,会返回上一级,当到达最上层是,点击返回退出应用,为了防止用户连续点击返回,导致应用退出,在用户点击返回到最上层时,如果再次点击返回,第一次不退出,并提升用户再次点击退出 ...
- Qt编写数据可视化大屏界面电子看板2-配色方案
一.前言 做完整个数据可视化大屏界面电子看板系统后,为了提升点逼格,需要提供好几套默认的风格样式以供选择,这样用户可以选择自己喜欢的配色方案来作为整个系统的颜色方案,去看了下市面上大部分的大屏电子看板 ...
- WdatePicker没有效果怎么办
1:如果WdatePicker没有效果时间输入框 或报 invalid property:firstDayOfWeek 个错误. 2:网上解决方法有很多,但很多都不规范. 解决方法:重新下载(下载地址 ...