very easy to understand java solution


  • 0
    F
        /**
         * e:[0,1,3,3,5,7,7,8,9]
         * f:[4,5]  5
         * g:[8]    8
         * h:[3,8]  3
         * i:[5,6,8,9]  9
         * n:[1,7,9,9]
         * o:[0,1,2,4]  1
         * r:[0,3,4]
         * s:[6,7]  7
         * t:[2,3,8]
         * u:[4]    4
         * v:[5,7]
         * w:[2]    2
         * x:[6]    6
         * z:[0]    0
         *
         * @param s
         * @return
         */
        public String originalDigits(String s) {
            int[] count = new int[10];
            for (char c : s.toCharArray()) {
                switch (c) {
                    case 'z':
                        count[0]++;
                        break;
                    case 'o':
                        count[1]++;
                        break;
                    case 'w':
                        count[2]++;
                        break;
                    case 'h':
                        count[3]++;
                        break;
                    case 'u':
                        count[4]++;
                        break;
                    case 'f':
                        count[5]++;
                        break;
                    case 'x':
                        count[6]++;
                        break;
                    case 's':
                        count[7]++;
                        break;
                    case 'g':
                        count[8]++;
                        break;
                    case 'i':
                        count[9]++;
                        break;
                    default:
                }
            }
            count[5] -= count[4];
            count[3] -= count[8];
            count[7] -= count[6];
            count[1] -= count[0] + count[2] + count[4];
            count[9] -= count[5] + count[6] + count[8];
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < count[i]; j++) {
                    result.append(i);
                }
            }
            return result.toString();
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.