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. 六十八、SAP中内表插入的三种方法之二,COLLECT的使用,用于计算数字字段之和

    一.使用COLLECT时,如果关键字没有,那么插入,如果有则求所有关键字列的和,代码如下 二.sy-index在循环中,每次循环从1开始递增 三.查看T_DATA数据 四.如下 五.循环时候,我们查看 ...

  2. Spark 2.x Troubleshooting Guide

    IBM在spark summit上分享的内容,包括编译spark源码,运行spark时候常见问题(缺包.OOM.GC问题.hdfs数据分布不均匀等),spark任务堆/thread dump 目录 编 ...

  3. 实验吧-杂项-flag.xls(notepad++查找)、保险箱(linux文件分解、密码破解)

    flag.xls 下载文件,用notepad++打开,查找flag就能找到flag. 保险箱(linux文件分解.密码破解) 将图片保存下来,用kali的binwalk分析,发现有rar文件,然后用f ...

  4. opencv python运动人体检测

    采用非极大值抑制,将重叠的框合并成一个. # import the necessary packages from imutils.object_detection import non_max_su ...

  5. [ACTF2020 新生赛]Exec

    0x00 知识点 命令执行 这里见了太多了..以前也写过: https://www.cnblogs.com/wangtanzhi/p/12246386.html 命令执行的方法大抵是加上管道符或者分号 ...

  6. Linux每日一练20200220

  7. 【Android】家庭记账本手机版开发报告四

    一.说在前面 昨天 对界面显示和逻辑结构进行完善 今天 1.添加菜单(查询.清除所有等) 2.使用滑动删除 问题 1.在做查询时获取SearchView时引 入包错误经过长时间的尝试后才修正 2.滑动 ...

  8. 每天一点点之vue框架开发 - vue-router路由在循环中携带参数

    场景:要实现一个标签云,通过循环把标签渲染,然后单击标签的时候实现跳转,跳转路由一样,通过唯一参数来实现请求不同的数据 因此,就需要在for循环中来携带参数,本节所讲的是路由使用对象的形式(别名)来实 ...

  9. 史上最好用的idea激活方法

    最近idea老出现激活一段时间然后就让重新激活的情况,每次都网上搜索一大堆激活方法,各种网址被封,各种插件不能用.就通过朋友介绍搞到一种方式,目前对于2018版本和2019版本都能激活并且正常使用.不 ...

  10. IE8兼容问题汇总

    一.css的hack写法 IE8 CSS hack 就是在属性后面加上 \9 或者 \0,代码如下: color:#FFF\0; /* IE8 */ color:#FFF\9; /* 所有IE浏览器( ...