从String中计算连续重复出现的字符数

时间:2012-04-27 07:12:40

标签: java

这是我的代码。

public static void countContinuosOccurence() {
    String first = "ABBCDDDEFGGH";
    StringBuffer result = new StringBuffer();
    int count = 1;
    for (int i = 1; i < first.length(); i++) {
        if (first.charAt(i) == (first.charAt(i - 1))) {
            count++;
        } else {
            if (count > 1) {

                result.append(String.valueOf(count) + first.charAt(i - 1));
            } else {
                result.append(first.charAt(i - 1));
            }
            count = 1;
        }
    }
    System.out.println("First String is:"+ first);
    System.out.println("Result is:" + result);
}

结果是:

First String is:ABBCDDDEFGGH
Result is:A2BC3DEF2G

缺少最后一个角色?愿有人帮我解决这个问题吗?

5 个答案:

答案 0 :(得分:3)

for循环结束后,您需要将 count 和最后一行字符的字符附加到结果中:

public static void countContinuosOccurence() {
    String first = "ABBCDDDEFGGH";
    StringBuffer result = new StringBuffer();
    int count = 1;
    int i;
    for (i = 1; i < first.length(); i++) {
        if (first.charAt(i) == (first.charAt(i - 1))) {
            count++;
        } else {
            if (count > 1) {    
                result.append(String.valueOf(count) + first.charAt(i - 1));
            } else {
                result.append(first.charAt(i - 1));
            }
            count = 1;
        }
    }

    // ADD THIS - to take care of the last run.
    if (count > 1) {    
        result.append(String.valueOf(count) + first.charAt(i - 1));
    } else {
        result.append(first.charAt(i - 1));
    }

    System.out.println("First String is:"+ first);
    System.out.println("Result is:" + result);
}

答案 1 :(得分:3)

不是最佳效果,但最简单的代码:

final String in = "ABBCDDDEFGGH" + '\u0000';
final StringBuilder b = new StringBuilder();
char prev = in.charAt(0);
int rpt = 0;
for (int i = 1; i < in.length(); i++) {
  final char curr = in.charAt(i);
  if (curr == prev) rpt++;
  else {
    b.append(rpt == 0? prev : "" + (rpt + 1) + prev);
    rpt = 0; prev = curr;
  }
}
System.out.println(b);

答案 2 :(得分:1)

public static void countContinuosOccurence() {

    String[] input = "ABBCDDDEFGGH".split("");
        String out = "";
        for (int i = 0; i < input.length; i++) {
            int repeatedCharCount = 1;
            String currentChr = input[i];
            if (!(i == input.length - 1)) {
                while (input[i].equals(input[i + 1])) {
                    repeatedCharCount++;
                    i++;
                }
            }
            out = out + repeatedCharCount + currentChr;
        }

        System.out.println(out);

}

答案 3 :(得分:0)

还有一个隐藏的问题,就是如果你以一个多次出现的序列终止,你就不会写任何东西。

解决此问题的最简单方法和您检测到的问题是在for block之后添加最终检查

[...]
    }
    int l = first.length();
    if (count > 1) {
        result.append(String.valueOf(count) + first.charAt(l - 1));
        } else {
            result.append(first.charAt(l - 1));
        }
    }
    System.out.println("First String is:"+ first);
    System.out.println("Result is:" + result);
}

答案 4 :(得分:0)

import java.util.*;
public class HelloWorld{
public static void main(String []args){
    System.out.println("Hello World");
    String first = "ABBCDDDEFGGHhhhhh456456456{{{67}}}";
StringBuffer result = new StringBuffer();
result.append(first);
System.out.println(result);
    Map<Character,Integer> map = new HashMap<Character,Integer>();
for(int i = 0; i < first.length(); i++) {
char c = first.charAt(i);
if (map.containsKey(c)) {
int cnt = map.get(c);
map.put(c, ++cnt);
} else {
map.put(c, 1);
}

}
Set set = map.entrySet(); 
// Get an iterator 
Iterator itr = set.iterator(); 
// Display elements 
while(itr.hasNext()) { 
Map.Entry me = (Map.Entry)itr.next(); 
System.out.print(me.getKey() + ": "); 
System.out.println(me.getValue()); 
} 
System.out.println("Hello World1");
 }
}