返回介绍

solution / 0400-0499 / 0423.Reconstruct Original Digits from English / README

发布于 2024-06-17 01:04:00 字数 3889 浏览 0 评论 0 收藏 0

423. 从英文中重建数字

English Version

题目描述

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

 

示例 1:

输入:s = "owoztneoer"
输出:"012"

示例 2:

输入:s = "fviefuro"
输出:"45"

 

提示:

  • 1 <= s.length <= 105
  • s[i]["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"] 这些字符之一
  • s 保证是一个符合题目要求的字符串

解法

方法一

class Solution:
  def originalDigits(self, s: str) -> str:
    counter = Counter(s)
    cnt = [0] * 10

    cnt[0] = counter['z']
    cnt[2] = counter['w']
    cnt[4] = counter['u']
    cnt[6] = counter['x']
    cnt[8] = counter['g']

    cnt[3] = counter['h'] - cnt[8]
    cnt[5] = counter['f'] - cnt[4]
    cnt[7] = counter['s'] - cnt[6]

    cnt[1] = counter['o'] - cnt[0] - cnt[2] - cnt[4]
    cnt[9] = counter['i'] - cnt[5] - cnt[6] - cnt[8]

    return ''.join(cnt[i] * str(i) for i in range(10))
class Solution {
  public String originalDigits(String s) {
    int[] counter = new int[26];
    for (char c : s.toCharArray()) {
      ++counter[c - 'a'];
    }
    int[] cnt = new int[10];
    cnt[0] = counter['z' - 'a'];
    cnt[2] = counter['w' - 'a'];
    cnt[4] = counter['u' - 'a'];
    cnt[6] = counter['x' - 'a'];
    cnt[8] = counter['g' - 'a'];

    cnt[3] = counter['h' - 'a'] - cnt[8];
    cnt[5] = counter['f' - 'a'] - cnt[4];
    cnt[7] = counter['s' - 'a'] - cnt[6];

    cnt[1] = counter['o' - 'a'] - cnt[0] - cnt[2] - cnt[4];
    cnt[9] = counter['i' - 'a'] - cnt[5] - cnt[6] - cnt[8];

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10; ++i) {
      for (int j = 0; j < cnt[i]; ++j) {
        sb.append(i);
      }
    }
    return sb.toString();
  }
}
class Solution {
public:
  string originalDigits(string s) {
    vector<int> counter(26);
    for (char c : s) ++counter[c - 'a'];
    vector<int> cnt(10);
    cnt[0] = counter['z' - 'a'];
    cnt[2] = counter['w' - 'a'];
    cnt[4] = counter['u' - 'a'];
    cnt[6] = counter['x' - 'a'];
    cnt[8] = counter['g' - 'a'];

    cnt[3] = counter['h' - 'a'] - cnt[8];
    cnt[5] = counter['f' - 'a'] - cnt[4];
    cnt[7] = counter['s' - 'a'] - cnt[6];

    cnt[1] = counter['o' - 'a'] - cnt[0] - cnt[2] - cnt[4];
    cnt[9] = counter['i' - 'a'] - cnt[5] - cnt[6] - cnt[8];

    string ans;
    for (int i = 0; i < 10; ++i)
      for (int j = 0; j < cnt[i]; ++j)
        ans += char(i + '0');
    return ans;
  }
};
func originalDigits(s string) string {
  counter := make([]int, 26)
  for _, c := range s {
    counter[c-'a']++
  }
  cnt := make([]int, 10)
  cnt[0] = counter['z'-'a']
  cnt[2] = counter['w'-'a']
  cnt[4] = counter['u'-'a']
  cnt[6] = counter['x'-'a']
  cnt[8] = counter['g'-'a']

  cnt[3] = counter['h'-'a'] - cnt[8]
  cnt[5] = counter['f'-'a'] - cnt[4]
  cnt[7] = counter['s'-'a'] - cnt[6]

  cnt[1] = counter['o'-'a'] - cnt[0] - cnt[2] - cnt[4]
  cnt[9] = counter['i'-'a'] - cnt[5] - cnt[6] - cnt[8]

  ans := []byte{}
  for i, c := range cnt {
    ans = append(ans, bytes.Repeat([]byte{byte('0' + i)}, c)...)
  }
  return string(ans)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文