[LC] 611. Compress String II
作者:互联网
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(); } }
标签:count,String,int,611,Compress,II,sb,charArr,string 来源: https://www.cnblogs.com/xuanlu/p/12340765.html