Anyone saw a clearer/cleaner C++ version than mine ? :)


  • 0
    F

    I think everyone got the idea but not everyone got the cleanest implementation.

    struct Solution {
        string originalDigits(string s) {
            int char_counts[26] = {0};
            for (auto c : s)
                char_counts[c - 'a'] ++;
            int digit_counts[10] = {0};
            digit_counts[0] = char_counts['z' - 'a'];
            digit_counts[2] = char_counts['w' - 'a'];
            digit_counts[4] = char_counts['u' - 'a'];
            digit_counts[6] = char_counts['x' - 'a'];
            digit_counts[8] = char_counts['g' - 'a'];
            digit_counts[1] = char_counts['o' - 'a'] - digit_counts[0] - digit_counts[2] - digit_counts[4];
            digit_counts[3] = char_counts['r' - 'a'] - digit_counts[0] - digit_counts[4];
            digit_counts[5] = char_counts['f' - 'a'] - digit_counts[4];
            digit_counts[7] = char_counts['s' - 'a'] - digit_counts[6];
            digit_counts[9] = char_counts['i' - 'a'] - digit_counts[5] - digit_counts[6] - digit_counts[8];
            string result;
            for (int i = 0; i < 10; ++ i)
                result.append(digit_counts[i], '0' + i);
            return result;
        }
    };
    

Log in to reply
 

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