简体   繁体   English

计算字符串中字符的连续重复出现

[英]Count continuous repeated occurrence of characters from String

This is my code. 这是我的代码。

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);
}

The result is: 结果是:

First String is:ABBCDDDEFGGH
Result is:A2BC3DEF2G

It is missing the last character? 它缺少最后一个字符吗? May someone help me to solve this? 有人可以帮我解决这个问题吗?

After the for loop ends, you'll need to append the count and the character of the last run of character(s) to the result: 在之后for循环结束,你需要追加和字符(县)结果的最后一次运行的特点

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);
}

Not top-performing, but simplest code: 不是最出色的代码,而是最简单的代码:

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);
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);

}

There is also a hidden problem, that is that if you are terminating with a sequence with more than one occurrence, you will not write anything. 还有一个隐藏的问题,就是如果您要终止一个序列且出现多次,那么您将不会编写任何内容。

The simplest way to solve this problem and the problem you detected is to add a final check after the for block 解决此问题和检测到的问题的最简单方法是在for块后添加最终检查

[...]
    }
    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);
}
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");
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM