leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0423.cpp (731B)


      1 class Solution {
      2   public:
      3     string originalDigits(const string &s) {
      4         string word[10] = {"zero", "two", "six", "eight", "three", "seven", "five", "four", "one", "nine"};
      5         uint8_t mapping[10] = {0, 2, 6, 8, 3, 7, 5, 4, 1, 9};
      6         uint32_t count[26] = {0}, digits[10] = {0};
      7         char *letters = "zwxghsvuoi";
      8         string res;
      9 
     10         for (const char c : s)
     11             count[c - 'a']++;
     12         for (int i = 0; i <= 9; i++) {
     13             const int cnt = digits[mapping[i]] = count[letters[i] - 'a'];
     14             for (const char c : word[i])
     15                 count[c - 'a'] -= cnt;
     16         }
     17         for (int i = 0; i <= 9; i++)
     18             res += string(digits[i], '0' + i);
     19 
     20         return res;
     21     }
     22 };