最大重复字符数

时间:2018-10-23 09:54:29

标签: java string character

我想获得最大重复字符数及其相关索引。我能够在给定的字符串及其索引中打印最大重复字符。但是,我无法打印重复字符的总数。以下是我的代码

public class MaxRepeating {

static char charactercountIndex(String str) {
    int len = str.length();
    int count = 0;

    char res = str.charAt(0);
    for (int i = 0; i < len; i++) {
        int cur_count = 0;
        for (int j = i + 1; j < len; j++) {
            if (str.charAt(i) != str.charAt(j))
                break;
            cur_count++;
        }

        if (cur_count > count) {
            count = cur_count;
            res = str.charAt(i);

        }
    }
    return res;

}

public static void main(String args[]) {

    String str = "aaaaaaccde";
    char s1 = charactercountIndex(str);
    str.indexOf(s1);
    System.out.println(str.indexOf(s1));
    System.out.println(charactercountIndex(str));

    }
  }

输出应为<0,6> 0是字符a的索引 6是字符串中出现的总时间字符“ a”

4 个答案:

答案 0 :(得分:1)

如果您愿意接受略有不同的方法,则可以使用正则表达式和流来使用一种相当简单的方法来完成此操作。我们可以尝试使用以下正则表达式将输入字符串拆分为类似字母的子字符串部分:

(?<=(.))(?!\\1)

然后,我们可以使用Collections.max查找集合中最大的字符串,最后使用String#indexOf查找该子字符串的索引。

String str = "aaaabbddddddddddddddddddddaaccde";
List<String> parts = Arrays.asList(str.split("(?<=(.))(?!\\1)"));
String max = Collections.max(parts, Comparator.comparing(s -> s.length()));
System.out.println("largest substring: " + max);
int index = str.indexOf(max);
System.out.println("index of largest substring: " + index);

largest substring: dddddddddddddddddddd
index of largest substring: 6

答案 1 :(得分:1)

我已经做了类似的事情:

static Entry<String, Integer> charactercountIndex(String str) {
    HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
    for (String letter : str.split("")) {
        if (stringIntegerHashMap.containsKey(letter)) {
            stringIntegerHashMap.put(letter, (stringIntegerHashMap.get(letter) + 1));
        } else {
            stringIntegerHashMap.put(letter, 1);
        }
    }

    Entry<String, Integer> maxEntry = null;

    for (Entry<String, Integer> entry : stringIntegerHashMap.entrySet()) {

        if (maxEntry == null
                || entry.getValue().compareTo(maxEntry.getValue()) > 0) {
            maxEntry = entry;
        }
    }

    return maxEntry;
}

public static void main(String args[]) {

    String str = "aaaabbddddddddddddddddddddaaccde";
    Entry<String, Integer> s1 = charactercountIndex(str);
    System.out.println(s1.getKey());
    System.out.println(s1.getValue());
}

如果有任何麻烦,请告诉我。

答案 2 :(得分:1)

您可以通过本地类实例(包含字符及其出现的实例)返回结果。我添加了一个本地课程// including ...

顺便说一句,我修复了您的代码(请参见public class MaxRepeating { private static CountResult charactercountIndex(String str) { int len = str.length(); char res = str.charAt(0); int count = 0; for (int i = 0; i < len; i++) { int cur_count = 1; // including the tested char (first occurence) for (int j = i + 1; j < len; j++) { if (str.charAt(i) != str.charAt(j)) break; cur_count++; } if (cur_count > count) { res = str.charAt(i); count = cur_count; } } return new CountResult(res, count); } private static class CountResult { private char maxChar; private int count; public CountResult(char maxChar, int count) { this.maxChar = maxChar; this.count = count; } public String toString() { return String.format("<" + maxChar + "," + count + ">"); } } public static void main(String args[]) { String str = "aaaaaaccde"; System.out.println(charactercountIndex(str)); } } 注释)。

您可以尝试检查here下的工作代码。

--no-line-editing

答案 3 :(得分:0)

您可以创建自己的类,而该类不会受到方法返回的参数的计数。

public class MyCharacter {
    private static int count;
    private static char character;
    private static int indexOf;

    public void characterCountIndex(String str) {
        int len = str.length();
        for (int i = 0; i < len; i++) {
            int cur_count = 1;
            for (int j = i + 1; j < len; j++) {
                if (str.charAt(i) != str.charAt(j))
                    break;
                cur_count++;
            }

            if (cur_count > count) {
                count = cur_count;
                character = str.charAt(i);
                indexOf = str.indexOf(character);
            }
        }
    }

    @Override
    public String toString() {
        return String.format("<%d, %d>", indexOf, count);
    }

    public static void main(String[] args) {
        String str = "aaaaaaccde";
        MyCharacter myCharacter = new MyCharacter();
        myCharacter.characterCountIndex(str);
        System.out.println(myCharacter);
    }
}