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请求: ...
随机推荐
- 02 CSS和DIV对界面优化
01 网站首页的优化 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- Could not load file or assembly "\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies.
Could not load file or assembly 'file:///C:\Program Files (x86)\SAP BusinessObjects\Crystal Repor ...
- 2019CCPC秦皇岛自我反省&部分题解
练了一年半了,第一次打CCPC,险些把队友坑了打铁,最后也是3题危险捡了块铜. 非常水的点双连通,我居然不相信自己去相信板子,唉,结果整来整去,本来半个小时能出的题,整到了3个小时,大失误呀,不然就可 ...
- Ubuntu安装配置mongodb
一:安装 -->官方教程 第一步: sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5 ...
- 【CUDA 基础】6.3 重叠内和执行和数据传输
title: [CUDA 基础]6.3 重叠内和执行和数据传输 categories: - CUDA - Freshman tags: - 深度优先 - 广度优先 toc: true date: 20 ...
- 转:获取时间点前后一定间隔的时间 __timeShift()
接口获取时间点前后一定间隔的时间函数: __timeShift(时间格式, 特定时间点(缺省当前时间),时间间隔,地区格式(默认),变量名( 可不填,填写后其他地方用${变量名}引用 )) 举例: 1 ...
- redis,memcached,mongodb之间的区别
Redis Redis的优点: 支持多种数据结构,如 string(字符串). list(双向链表).dict(hash表).set(集合).zset(排序set).hyperloglog(基数估算) ...
- CF258B
CF258B 题意: 7个人在 $ [1,m] $ 区间内取数,求第一个人选的数的4和7的个数大于其他人的4和7个数总和的方案数. 解法: 要求输入的 $ m $ 可以很大,而且需要按位考虑每隔人的贡 ...
- Leetcode题目分类整理
一.数组 8) 双指针 ---- 滑动窗口 例题: 3. Longest Substring Without Repeating Characters 描述:Given a string, find ...
- Java通过过滤器修改header
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...