给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDAA会被编码成4A1B2C1D2A。

package wangyitest;


import java.util.Scanner;


public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
char[] array = s.toCharArray();
StringBuilder sb = new StringBuilder();
int count = 1;
if (array.length == 0) {
 
System.out.print("null");
 
}
if (array.length == 1) {
System.out.print(""+ count + array[0]);
 
}
for (int i = 1; i < array.length; i++) {
 
if(array[i]==array[i-1]){
count++;
}else{
sb.append(count).append(array[i - 1]);
count = 1;
}
if(i == array.length - 1){
sb.append(count).append(array[i]);
}
}
 
System.out.print(sb.toString());
}



}

你可能感兴趣的:(给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDAA会被编码成4A1B2C1D2A。)