Clean O(n) Java Solution


  • 0
    A
    public class Solution {
        public String originalDigits(String s) {
            int[] count = new int[26];
            int[] nums = new int[10];
            for (char c : s.toCharArray()) {
                count[c - 'a']++;
            }
            nums[0] = count['z' - 'a'];
            nums[2] = count['w' - 'a'];
            nums[4] = count['u' - 'a'];
            nums[6] = count['x' - 'a'];
            nums[8] = count['g' - 'a'];
            nums[3] = count['h' - 'a'] - nums[8];
            nums[7] = count['s' - 'a'] - nums[6];
            nums[5] = count['v' - 'a'] - nums[7];
            nums[9] = count['i' - 'a'] - nums[5] - nums[6] - nums[8];
            nums[1] = count['n' - 'a'] - nums[7] - nums[9] * 2;
            StringBuilder res = new StringBuilder();
            for (int i = 0; i < 10; i++) {
                while (nums[i]-- > 0) {
                    res.append(i);
                }
            }
            return res.toString();
        }
    }
    

    Avoid using many if conditions>..


Log in to reply
 

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