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. nidlist 问题

    错误问题如下: 解决方案: Dao文件 boolean DeleteList(String nidList); 改为: boolean DeleteList(@Param("nidList& ...

  2. PyCharm下创建并运行我们的第一个Django项目

    PyCharm下创建并运行我们的第一个Django项目 准备工作: 假设读者已经安装好python 2x或3x,以及安装好Django,以及Pycharm 1. 创建一个新的工程 第一次运行Pycha ...

  3. HDU - 6043 KazaQ's Socks(找规律)

    题意:有n双袜子,编号1到n,放在衣柜里,每天早晨取衣柜中编号最小的袜子穿,晚上将这双袜子放在篮子里,当篮子里有n-1双袜子时,清洗袜子,直到第二天晚上才洗好,并将洗好的袜子重新放回衣柜. 分析:规律 ...

  4. 使用Linux系统,是一种什么体验?

    导读 同事,从事嵌入式软件开发多年,主要开发环境用的就是linux,最疯狂的一段时间直接把系统装成linux系统,然后在linux下面虚拟一个windows操作系统,主要有些事情必须在windows才 ...

  5. h5-伸缩布局

    1.html代码 <div class="box"> <div class="first">1</div> <div ...

  6. 虚拟机vmware vmnet8 未识别(转)

    原文链接:https://blog.csdn.net/windows_q/article/details/50678646

  7. 尝试用kotlin做一个app(一)

    1.先添加一下anko库 依赖:implementation "org.jetbrains.anko:anko:$anko_version" 版本:ext.anko_version ...

  8. POJ 2006:Litmus Test 化学公式

    Litmus Test Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1709   Accepted: 897 Descri ...

  9. kube-proxy详解

    KUBE_LOGTOSTDERR="--logtostderr=true"KUBE_LOG_LEVEL="--v=4"NODE_HOSTNAME="- ...

  10. JS-语句五

    for循环的实例 1.九九乘法表: 1*1  1*2  1*3        1*2  2*2  2*3        1*3  2*3  3*3        1*4  2*4  4*3       ...