Given a string, replace adjacent, repeated characters with the character followed by the number of repeated occurrences.

Assumptions

  • The string is not null

  • The characters used in the original string are guaranteed to be ‘a’ - ‘z’

Examples

  • “abbcccdeee” → “a1b2c3d1e3”

public class Solution {
public String compress(String input) {
// Write your solution here
char[] charArr = input.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < charArr.length; i++) {
char cur = charArr[i];
int count = 1;
while (i + 1 < charArr.length && charArr[i + 1] == charArr[i]) {
count += 1;
i += 1;
}
sb.append(cur).append(count);
}
return sb.toString();
}
}

[Algo] 611. Compress String II的更多相关文章

  1. [Algo] 175. Decompress String II

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

  2. [CareerCup] 1.5 Compress String 压缩字符串

    1.5 Implement a method to perform basic string compression using the counts of repeated characters. ...

  3. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  4. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  5. Reverse Words in a String I & Reverse Words in a String II

    Reverse Words in a String I Given an input string, reverse the string word by word. For example,Give ...

  6. leadcode 541. Reverse String II

    package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...

  7. NYOJ 1067 Compress String(区间dp)

    Compress String 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描写叙述 One day,a beautiful girl ask LYH to help he ...

  8. LeetCode 541. 反转字符串 II(Reverse String II)

    541. 反转字符串 II 541. Reverse String II

  9. 【leetcode_easy】541. Reverse String II

    problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...

随机推荐

  1. 074-PHP数组元素相乘

    <?php $arr1=array(3,4,5,6,'7',TRUE); //等价于 3*4*5*6*7*1=2520 $arr2=array(3,4,5,6,'7','hello'); //等 ...

  2. 吴裕雄--天生自然C++语言学习笔记:C++ 异常处理

    异常是程序在执行期间产生的问题.C++ 异常是指在程序运行时发生的特殊情况,比如尝试除以零的操作. 异常提供了一种转移程序控制权的方式.C++ 异常处理涉及到三个关键字:try.catch.throw ...

  3. 吴裕雄--天生自然C++语言学习笔记:C++ 字符串

    C++ 提供了以下两种类型的字符串表示形式: C 风格字符串 C++ 引入的 string 类类型 C 风格的字符串起源于 C 语言,并在 C++ 中继续得到支持.字符串实际上是使用 null 字符 ...

  4. python爬取网页文本、图片

    从网页爬取文本信息: eg:从http://computer.swu.edu.cn/s/computer/kxyj2xsky/中爬取讲座信息(讲座时间和讲座名称) 注:如果要爬取的内容是多页的话,网址 ...

  5. UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

    很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...

  6. Mac 终端命令使用自动补全时忽略大小写设置

    Mac 终端在使用 tab 补全命令时默认大小写敏感,通过以下设置方法可以忽略大小写,方法同样适用于 iTerm2. 打开终端输入以下命令: nano .inputrc 将以下命令复制进去: set ...

  7. 浅谈__slots__

    __slots__在python中是扮演属性声明(Attribute Declaration)的角色,当然属性本身不用声明,只需要在使用前赋值即可,但是实际上,属性的应用远非在使用前赋值就行,所以有a ...

  8. MySQL授权用户登录访问指定数据库

    使用Navicat等客户端工具,选中需要共享的数据库,点击查询>新建查询 1.写SQL语句:GRANT ALL PRIVILEGES ON * TO 'test'@'%'IDENTIFIED B ...

  9. 理解Production- Ready特性

    1.外部配置(externalized configuration) 1).基于环境变量的配置 2).基于YAML的配置 3).默认配置值 2.健康检查(health checks) 1).它是否有一 ...

  10. NoSQL:

    NoSQL:NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL". 在现代的计算系统上每天网络上都会产生庞大的数据量. 这些数据有很大一部分是由关系数据 ...