class Solution {
public:
string originalDigits(string s) {
}
};
423. 从英文中重建数字
给你一个字符串 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
保证是一个符合题目要求的字符串原站题解
java 解法, 执行用时: 6 ms, 内存消耗: 42 MB, 提交时间: 2022-12-04 11:41:56
class Solution { static String[] ss = new String[]{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; static int[] priority = new int[]{0, 8, 6, 3, 2, 7, 5, 9, 4, 1}; public String originalDigits(String s) { int n = s.length(); int[] cnts = new int[26]; for (int i = 0; i < n; i++) cnts[s.charAt(i) - 'a']++; StringBuilder sb = new StringBuilder(); for (int i : priority) { int k = Integer.MAX_VALUE; for (char c : ss[i].toCharArray()) k = Math.min(k, cnts[c - 'a']); for (char c : ss[i].toCharArray()) cnts[c - 'a'] -= k; while (k-- > 0) sb.append(i); } char[] cs = sb.toString().toCharArray(); Arrays.sort(cs); return String.valueOf(cs); } }
java 解法, 执行用时: 18 ms, 内存消耗: 42.1 MB, 提交时间: 2022-12-04 11:41:37
class Solution { public String originalDigits(String s) { Map<Character, Integer> c = new HashMap<Character, Integer>(); for (int i = 0; i < s.length(); ++i) { char ch = s.charAt(i); c.put(ch, c.getOrDefault(ch, 0) + 1); } int[] cnt = new int[10]; cnt[0] = c.getOrDefault('z', 0); cnt[2] = c.getOrDefault('w', 0); cnt[4] = c.getOrDefault('u', 0); cnt[6] = c.getOrDefault('x', 0); cnt[8] = c.getOrDefault('g', 0); cnt[3] = c.getOrDefault('h', 0) - cnt[8]; cnt[5] = c.getOrDefault('f', 0) - cnt[4]; cnt[7] = c.getOrDefault('s', 0) - cnt[6]; cnt[1] = c.getOrDefault('o', 0) - cnt[0] - cnt[2] - cnt[4]; cnt[9] = c.getOrDefault('i', 0) - cnt[5] - cnt[6] - cnt[8]; StringBuffer ans = new StringBuffer(); for (int i = 0; i < 10; ++i) { for (int j = 0; j < cnt[i]; ++j) { ans.append((char) (i + '0')); } } return ans.toString(); } }
golang 解法, 执行用时: 8 ms, 内存消耗: 4.9 MB, 提交时间: 2022-12-04 11:41:23
func originalDigits(s string) string { c := map[rune]int{} for _, ch := range s { c[ch]++ } cnt := [10]int{} cnt[0] = c['z'] cnt[2] = c['w'] cnt[4] = c['u'] cnt[6] = c['x'] cnt[8] = c['g'] cnt[3] = c['h'] - cnt[8] cnt[5] = c['f'] - cnt[4] cnt[7] = c['s'] - cnt[6] cnt[1] = c['o'] - cnt[0] - cnt[2] - cnt[4] cnt[9] = c['i'] - 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) }
javascript 解法, 执行用时: 84 ms, 内存消耗: 45.4 MB, 提交时间: 2022-12-04 11:41:05
/** * @param {string} s * @return {string} */ var originalDigits = function(s) { const c = new Map(); for (const ch of s) { c.set(ch, (c.get(ch) || 0) + 1); } const cnt = new Array(10).fill(0); cnt[0] = c.get('z') || 0; cnt[2] = c.get('w') || 0; cnt[4] = c.get('u') || 0; cnt[6] = c.get('x') || 0; cnt[8] = c.get('g') || 0; cnt[3] = (c.get('h') || 0) - cnt[8]; cnt[5] = (c.get('f') || 0) - cnt[4]; cnt[7] = (c.get('s') || 0) - cnt[6]; cnt[1] = (c.get('o') || 0) - cnt[0] - cnt[2] - cnt[4]; cnt[9] = (c.get('i') || 0) - cnt[5] - cnt[6] - cnt[8]; const ans = []; for (let i = 0; i < 10; ++i) { for (let j = 0; j < cnt[i]; ++j) { ans.push(String.fromCharCode(i + '0'.charCodeAt())); } } return ans.join(''); };
python3 解法, 执行用时: 48 ms, 内存消耗: 15.2 MB, 提交时间: 2022-12-04 11:40:49
class Solution: def originalDigits(self, s: str) -> str: c = Counter(s) cnt = [0] * 10 cnt[0] = c["z"] cnt[2] = c["w"] cnt[4] = c["u"] cnt[6] = c["x"] cnt[8] = c["g"] cnt[3] = c["h"] - cnt[8] cnt[5] = c["f"] - cnt[4] cnt[7] = c["s"] - cnt[6] cnt[1] = c["o"] - cnt[0] - cnt[2] - cnt[4] cnt[9] = c["i"] - cnt[5] - cnt[6] - cnt[8] return "".join(str(x) * cnt[x] for x in range(10))