美文网首页
423. Reconstruct Original Digits

423. Reconstruct Original Digits

作者: jluemmmm | 来源:发表于2021-11-20 14:41 被阅读0次

    给你一个字符串 s ,其中包含字母顺序打乱的用英文单词表示的若干数字(0-9)。按 升序 返回原始的数字。

    • Runtime: 112 ms, faster than 55.04%
    • Memory Usage: 42 MB, less than 79.84%
    • 时间复杂度O(n),空间复杂度O(1)
    /**
     * @param {string} s
     * @return {string}
     */
    var originalDigits = function(s) {
      let map = {};
      for (const item of s) {
        map.hasOwnProperty(item) ? map[item]++ : map[item] = 1;
      }
      console.log(map)
      
      let f = Array(10).fill(0);
      f[0] = map['z'] || 0;
      f[2] = map['w'] || 0;
      f[4] = map['u'] || 0;
      f[6] = map['x'] || 0;
      f[8] = map['g'] || 0;
      
      f[1] = (map['o'] || 0) - f[0] - f[2] - f[4];
      f[3] = (map['h'] || 0) - f[8];
      f[5] = (map['f'] || 0) - f[4];
      f[7] = (map['s'] || 0) - f[6];
      f[9] = (map['i'] || 0) - f[6] - f[8] - f[5];
      
      let res = '';
      for (let i = 0; i < 10; i++) {
        res += (i + '').repeat(f[i]);
      }
      return res;
    };
    

    相关文章

      网友评论

          本文标题:423. Reconstruct Original Digits

          本文链接:https://www.haomeiwen.com/subject/cmketrtx.html