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 SP8093 后缀自动机+树状数组+dfs序
这题解法很多,简单说几个: 1. 线段树合并,时间复杂度是 $O(nlog^2n)$ 的. 2. 暴力跳 $fail,$ 时间复杂度 $O(n\sqrt n),$ 比较暴力. 3. 建立后缀树后在 $ ...
- jQuery系列(十):事件对象
1.事件对象 Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态. (1)什么时候会产生Event 对象呢? 例如: 当用户单击某个元素的时候,我们给 ...
- C语言基础语法之指向函数的指针
指针是C语言的精髓,对于初学者来讲,指针是C语言语法学习中比较难的知识点,而这里面指向函数的指针更是不太容易理解. 下面给大家讲下怎样学习理解C语言中指向函数的指针及编程方法和使用例子. 注意:这是一 ...
- Go语言编程中字符串切割方法小结
1.func Fields(s string) []string,这个函数的作用是按照1:n个空格来分割字符串最后返回的是[]string的切片 复制代码代码如下: import ( "fm ...
- Tomcat的server.xml
慕课网:https://www.imooc.com/video/19201 Server:指整个tomcat服务器,它其中包含多个组件,它主要负责管理和启动各个service,同时监听8005端发过来 ...
- Linux设备驱动程序 之 顺序锁
当要保护的资源很小,很简单,会频繁的被访问而且写入访问很少的且必须快速时(即读不允许让写饥饿),就可以使用顺序锁(seqlock):从本质上讲,顺序锁会允许读取者对资源的自由访问,但需要读取者检查是否 ...
- sentinel控制台监控数据持久化【MySQL】
根据官方wiki文档,sentinel控制台的实时监控数据,默认仅存储 5 分钟以内的数据.如需持久化,需要定制实现相关接口. https://github.com/alibaba/Sentinel/ ...
- 约束布局ConstraintLayout详解
约束布局ConstraintLayout详解 转 https://www.jianshu.com/p/17ec9bd6ca8a 目录 1.介绍 2.为什么要用ConstraintLayout 3.如何 ...
- EclipseADT编写单元测试代码的步骤
1. 写一个类 extends AndroidTestCase 2. 写一个测试方法 a.必须是public b.必须抛出异常给操作系统 public void textAdd()thr ...
- smarty section 循环不同的四个样式
<div class="moban_spzs"> <{section name=goodslist loop=$strdata6}> <{if $sm ...