【问题标题】:java, how to find the amount of letter in a string without repeatsjava,如何在不重复的情况下找到字符串中的字母数量
【发布时间】:2017-06-25 08:36:13
【问题描述】:

我正在尝试构建一个函数,它获取一串字母,并打印字符串中每个字母的数量。 例如: 输入:字符串=“aaabbaccxyxyx” 输出:4a2b2c3x2y

这是我想出的:

public class Q1 {
    public static String numLetters(String s){
        String end = new String();
        int counter = 0;
        char c,d;
        for(int i=0; i<s.length();i++){
            c = s.charAt(i);
            for(int j=0; j<s.length();j++){
                d = s.charAt(j);
                if(c == d){
                    counter++;
                }
            }
            end = end + counter+c;
            counter = 0;
        }

        return end;
    }

但是,这是输出:4a4a4a2b2b4a2c2c3x2y3x2y3x 很多重复..

任何帮助如何使它正确? 请记住,该函数需要返回一个 string,而不仅仅是打印出来。 谢谢! =)

【问题讨论】:

标签: java string int letters


【解决方案1】:

我会创建一个int 数组来保存字符串中每个字母的计数。因为有26个字母,所以数组的length应该是26

public static String numLetters(String s) {
    int[] count = new int[26];
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        count[(int)(c - 'a')]++;
    }
    String ans = "";
    for (int i = 0; i < 26; i++) {
        if (count[i] != 0) {
            ans += String.valueOf(count[i]) + (char)(i + 'a');
        }
    }
    return ans;
}

【讨论】:

  • 这可能有用,但是看头部“public static String”,该函数需要返回一个字符串。
  • @ItayBenMoshe 我现在将编辑我的答案,如果您觉得有用,请将其标记为 true
  • 干得好!你忘了创建一个 c 'char' 变量,但其他一切都很棒!谢谢! :)
  • 你应该为ans使用StringBuilder,这样最后一个循环会更有效率。字符串连接需要将字符串复制到一个新数组中,而编译器在循环中无法优化。
【解决方案2】:

一个简单的变体可能如下所示:

public static String countChars(String arg) {
    String res = "";
    boolean[] counted = new boolean[arg.length()];
    for (int i = 0; i < counted.length; i++) {
        if (!counted[i]) {
            char c = arg.charAt(i);
            int counter = 1;
            for (int j = i + 1; j < counted.length; j++) {
                if (arg.charAt(j) == c) {
                    counter++;
                    counted[j] = true;
                }
            }
            res += counter + "" + c;
        }
    }
    return res;
}

【讨论】:

  • O(n^2) 平均,看我的回答
【解决方案3】:

如果你想保持原来的结构,我建议使用StringBuilder,这样你就可以删除你已经看到的字符。如果你删除一个字符,你必须调整你的索引ij

public static String numLetters(String str){
    StringBuilder s = new StringBuilder(s);
    String end = new String();
    int counter = 0;
    char c,d;
    for(int i=0; i<s.length();i++){
        c = s.charAt(i);
        for(int j=0; j<s.length();j++){
            d = s.charAt(j);
            if(c == d){
                s.deleteCharAt(j);
                if (i >= j) i--;
                j--;
                counter++;
            }
        }
        end = end + counter+c;
        counter = 0;
    }

    return end;
}

【讨论】:

  • 效率不高,我不想使用这种方法
【解决方案4】:

试试这个:

int count = StringUtils.countMatches("a.b.c.d", ".");

【讨论】:

  • 对不起,我改成Java方案了
  • 这还是没有回答问题,好像抄袭自here。如果您认为此问题重复,请将该问题标记为此类问题。
  • 这不是这个问题的答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-11
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多