【问题标题】:How do I count the characters in a string and then sort them alphabetically?如何计算字符串中的字符,然后按字母顺序排序?
【发布时间】:2020-02-11 00:10:11
【问题描述】:

我正在接受用户的输入。我已经完成了那一点。输入可以是一个单词,甚至可以是一个保存为字符串的句子。然后我想要做的是计算字母出现在输入中的次数,并按字母顺序排序。

示例输入:

learning to program

示例输出:

a 2
e 1
g 2
i 1

【问题讨论】:

  • 请注意,您实际上还没有问过有关您的代码的编程问题。仅仅想做某事还不足以发布到 Stackoverflow:到目前为止,您尝试过什么,以及为什么没有奏效?并观察asking a good question 的“搜索和研究优先”部分,您是否首先在网上搜索过这个?因为如果我搜索“java 对字符串中的字符进行排序”,我会立即得到大量点击,所有这些都解释了如何做到这一点。

标签: java sorting count alphabetical


【解决方案1】:

这里解释了如何计算字符串中的出现次数: https://stackoverflow.com/a/881111/8935250 我试图在此代码小提琴中模仿您的示例输出。

我使用的方法: Sort Map

const string = "learning to program"

function count(character) {
	return string.split(character).length
}

map = string.split("").map(c => {
	return {c, count: count(c)}
})

map.sort((a,b) => b.count - a.count)

console.log(map)
console.log(string.split("").sort((a,b) => string.split(b).length - string.split(a).length))

也应该完成这项工作,但不显示出现的情况。

【讨论】:

    【解决方案2】:

    我为你写了一些代码,应该可以解决问题:)

     String name = "doodleice";
    
        HashMap<Character, Integer> charMap = new HashMap<>();
    
        char[] charArray = name.toCharArray();
    
        for(int i = 0; i < charArray.length; i++){
            if(charMap.containsKey(charArray[i])){
                charMap.put(charArray[i], charMap.get(charArray[i]) + 1);
            }
            else{
                charMap.put(charArray[i], 1);
            }
        }
    
        ArrayList<Character> charList = new ArrayList<>();
        for(Map.Entry<Character, Integer> entry: charMap.entrySet()){
            charList.add(entry.getKey());
        }
    
        Collections.sort(charList);
    
        for(int i = 0; i < charList.size(); i++){
            System.out.println(charList.get(i) + " " + charMap.get(charList.get(i)));
        }
    

    【讨论】:

      【解决方案3】:

      排序字符串“aabbbcddeeee”的简单解决方案之一,然后输出:a2b3c1d2e4

      public static void main(String[] args) {
                  
          String input = "aabbbcddeeee"; // output: a2b3c1d2e4
          int count = 0;
          int i = 0;
          int k = 0;
          
          for (i = 0; i < input.length(); i++) {
              
              for (int j = i; j < input.length(); j++) {
                  
                  if (input.charAt(i) == input.charAt(j)) {
                      
                      count = count + 1;
                      k++;
                  } else
                      break;
              }
              System.out.print(input.charAt(i));
              System.out.print(count+"\n");
              i = k - 1;
              count = 0;     // reset counter
          }
      }
      

      【讨论】:

        【解决方案4】:

        这个怎么样;

        const test ="the quick brown fox jumps over the lazy dog";
        const letterMap = {};
        [...test].forEach(x=>{letterMap[x]?letterMap[x]++:(letterMap[x]=1)});
        console.log (Object.keys(letterMap).sort().map((key) => [key, letterMap[key]]));

        输出;

        [[" ",8],["a",1],["b",1],["c",1],["d",1],["e",3],["f",1],["g",1],["h",2],["i",1],["j",1],["k",1],["l",1],["m",1],["n",1],["o",4],["p",1],["q",1],["r",2],["s",1],["t",2],["u",2],["v",1],["w",1],["x",1],["y",1],["z",1]]
        

        【讨论】:

          猜你喜欢
          • 2015-05-07
          • 1970-01-01
          • 2014-01-10
          • 2021-12-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-16
          • 1970-01-01
          相关资源
          最近更新 更多