当前位置: 首页 > 知识库问答 >
问题:

计算最常出现的字符

丁善
2023-03-14

在用户输入的字符串中,我很难使用计数器。代码定位最常见的字符,但我可以将计数器放在哪里,它计算最常见的字符。Java,请使用当前代码。这是最后一个方法。

import java.util.*;

public class newTest {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        System.out.println("Please enter a one line sentence:");
        String words = scnr.nextLine();

        findAlphabetMode(words);

    }

    public static void findAlphabetMode(String input){
        int[] freq = new int[input.length()];
        char mode = input.charAt(0);
        boolean noMode = true;
        int counter = 0;
        int steadyCount = 0;

        char string[] = input.toCharArray();

    for (int i = 0; i < string.length; i++){
     freq[i] = 1;
        for(int j = i + 1; j < string.length; j++){
          if(string[i] == string[j] && string[i] != ' ' && string[i] != 0){
            freq[i]++;
            //counter++;
          }
          if(counter > 1){
              if(counter > steadyCount){
                  steadyCount = counter + 1;
                  counter = 0;
              }
          }
          if(string[i] == string[j]){
            noMode = false;
            string[j] = 0;
            counter++;
          }
        }
    }
    int max = freq[0];

    for(int i = 0; i < freq.length; i++){
      if(max < freq[i]){
        max = freq[i];
        mode = string[i];
        noMode = false;
      }
    }

        if (noMode) {
            System.out.println("Mode: No Mode");
        }
        else {
            System.out.println("The letter " + mode + " occurs " + steadyCount + " times");
        }
    }
}
`

共有3个答案

端木飞
2023-03-14

即使没有流,也不能那么复杂:

public static void findAlphabetMode(String input) {
    var freq = new int['z'-'a'+1];
    for (var ch : input.toLowerCase().toCharArray()) {
        if (ch >= 'a' && ch <= 'z') {
            freq[ch-'a'] += 1;
        }
    }
    var max = 0;  // index to maximum freq
    for (var i = 1; i < freq.length; i++) {
        if (freq[i] > freq[max]) {
            max = i;
        }
    }
    var ch = (char) ('a' + max);
    System.out.printf("found %d times %c%n", freq[max], ch);
}

不难将第二个循环合并到第一个循环中:

public static void findAlphabetMode(String input) {
    var freq = new int['z'-'a'+1];
    var max = 0;  // index to maximum freq
    for (var ch : input.toLowerCase().toCharArray()) {
        if (ch >= 'a' && ch <= 'z') {
            if (++ freq[ch-'a'] > max) {
                max = ch - 'a';
            }
        }
    }
    var ch = (char) ('a' + max);
    System.out.printf("found %d times %c%n", freq[max], ch);
}

注:转换为小写后,仅统计字母a-z

戈宏义
2023-03-14

您不需要单独递增计数器,因为您已经在计算数组中每个字符的频率,您可以简单地在数组上循环并找到最大出现字符。以下是代码的简化版本:

import java.util.*;

public class newTest {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        System.out.println("Please enter a one line sentence:");
        String words = scnr.nextLine();
        findAlphabetMode(words);
    }

    public static void findAlphabetMode(String input){
        int[] freq = new int[input.length()];
        char mode = input.charAt(0);
        char string[] = input.toCharArray();
        for (int i = 0; i < string.length; i++){
         freq[i] = 1;
         for(int j = i + 1; j < string.length; j++){
            if(string[i] == string[j]){
              freq[i]++;
            }
         }
      }
      int max = freq[0];

      for(int i = 0; i < freq.length; i++){
        if(max < freq[i]) {
         max = freq[i];
         mode = string[i];
       }
     }
     System.out.println("The letter " + mode + " occurs " + max + " times");
    }
}
令狐翰
2023-03-14

您不需要嵌套的for循环。这使得您的算法成为O(N^2),即当您的输入字符串长度加倍时,所用的时间增加了四倍。

您可以将输入字符串中的每个字母放入映射中

public static void findAlphabetMode(String input) {
    input.chars()
        .boxed()
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
        .entrySet()
        .stream()
        .max(Map.Entry.comparingByValue())
        .ifPresentOrElse(
                e -> System.out.printf("The letter %c occurs %d times%n", e.getKey(), e.getValue()),
                () -> System.out.println("No mode"));
}
 类似资料:
  • 本文向大家介绍JavaScript实现计算字符串中出现次数最多的字符和出现的次数,包括了JavaScript实现计算字符串中出现次数最多的字符和出现的次数的使用技巧和注意事项,需要的朋友参考一下 “计算出字符串中出现次数最多的字符是什么,出现了多少次?” 看到这个需求,我想大多数人应该首先想到的是转换成数组,再做处理,当然是可以解决问题的,然后这里提供一个巧妙的算法设计,无需转数组,可以很快解决问

  • 问题内容: 我在mysql表中有一列,其数据类型为INT(11)。 如何搜索以获取此列中出现次数最多的前10个值? 问题答案:

  • 我需要返回一个字典,该字典计算预定列表中每个字母出现的次数。问题是我需要把大小写字母都算成一样,所以我不能用.low或.uper。 因此,例如,如果“t”是要搜索的字母,“This is a Python String”应该返回{nt':3}。 这是我目前所掌握的... 其中'letters'是条件,fullText是我正在搜索的字符串。 这里很明显的问题是,如果测试是“t”而不是“t”,我的代码

  • 问题内容: 我堆了一会儿。我尝试调试,但找不到解决方案。我正在尝试计算数字的出现。所以我的问题是当我打印输出时 代替 因此,如果数字出现的次数超过1,则应该只说一次,而不是出现的次数。欢呼这是代码 问题答案: 另一个选项是番石榴的Multiset类,它将为您跟踪计数: 在这里,Multiset,HashMultiset和Ints都是番石榴类。 请注意,Multiset通过使用Map和counter

  • 本文向大家介绍C++计算每个字符出现的次数,包括了C++计算每个字符出现的次数的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了C++计算每个字符出现的次数的实现代码,供大家参考,具体内容如下 以上就是本文的全部内容,希望对大家的学习有所帮助。

  • 问题内容: 计算字符串中字符出现次数的最简单方法是什么? 例如,计算出现在其中的次数 问题答案: 返回sub范围中的子字符串不重叠的次数。可选参数并以片表示法解释。