Turn String aaaabbbbffffd into a4b4d3

前端 未结 5 1530
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 04:46

I\'m trying to get a head start on practicing interview questions and I came across this one:

Turn String aaaabbbbffffd into a4b4d3

You would basically want t

5条回答
  •  庸人自扰
    2021-01-05 05:24

    Here is my solution

    public String countChars(String in){
     LinkedHashMapMap map = new LinkedHashMap();
     for(char c: in.toCharArray()){
       Integer count =  map.get(c);
       if(count==null){
        count=0;
       }
       count++;
       map.put(c,count);
     }
     String out ="";
     for(Entry e : map.entrySet()){
        out += e.getKey()+e.getValue();
     }
     return out;
    }
    

提交回复
热议问题