Given a string
Example 2:
Constraints:
s
containing an out-of-order English representation of digits 0-9
, return the digits in ascending order. Example 1:Input: s = “owoztneoer”
Output: “012”
Example 2:
Input: s = “fviefuro”
Output: “45”
Constraints:
1 <= s.length <= 10<sup>5</sup>
s[i]
is one of the characters ["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"]
. s
is *guaranteed to be valid.题目大意:
数字用英语单词表示如12 -> onetwo, 但打乱顺序otwone. 求如何获得原数字
算法思路:
统计每个字母的个数,根据个数来决定数字
规律见代码: 有些字母但唯一的,如two,w可以知道数字含2
但有些字母是多个数字的和如s,six和seven都含有s,减去用上述的six的个数就知道seven的个数
Python代码:
1 | def originalDigits(self, s: str) -> str: |
算法分析:
时间复杂度为O(10n)
,空间复杂度O(n)