Turn String aaaabbbbffffd into a4b4d3

前端 未结 5 1502
佛祖请我去吃肉
佛祖请我去吃肉 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:23

    Here is the code that I tried.

    I think you can't ask for a simpler code than this.

        String s = "aaaabbbbffffd", modified = "";
        int len = s.length(),  i = 0,  j = i + 1,  count = 1;
        char[] c = s.toCharArray();
        for (; i < len; i = j) {
            count = 1;
                for (; j < len; j++) 
                     if (c[i] == c[j]) 
                     count++;
                        else {
                        j++;
                        break;
                        }
            modified += c[i] + "" + count;
        }
        System.out.println(modified);
    

提交回复
热议问题