当前位置:网站首页>[leetcode problem solving report] 423 Rebuild numbers from English
[leetcode problem solving report] 423 Rebuild numbers from English
2022-07-18 21:17:00 【Seven water Shuliang】
[LeetCode Problem solving report ] 423. Reconstructing Chinese from English
One 、 subject
1. Title Description
- Reconstructing Chinese from English
difficulty : secondary
Give you a string s , It contains a number of numbers represented by English words in disordered alphabetical order (0-9). Press Ascending Returns the original number .
Example 1:
Input :s = "owoztneoer"
Output :"012"
Example 2:
Input :s = "fviefuro"
Output :"45"
Tips :
1 <= s.length <= 105s[i]by[“e”,“g”,“f”,“i”,“h”,“o”,“n”,“s”,“r”,“u”,“t”,“w”,“v”,“x”,“z”]One of these characterssThe guarantee is a string that meets the requirements of the topic
2. Original link
link : 423. Reconstructing Chinese from English
Two 、 Problem solving report
1. Thought analysis
An interesting question , Just got it, there will be no idea .
I have reserved some useless notes for this question , But this part reflects my thinking process .
- The title is guaranteed to be a legal string , First of all, I will think of counting the number of occurrences of each character, and then dfs Enumerate every word to try . Looked at the eye data range 1e5 Obviously not .
- Even if it's dfs Enumerating each word also counts each word , The number of occurrences of each character .
- Is it meaningful to count the length of each word ? It seems useless .
- Then new ideas appeared : Does a character exist in only one word , such as z, Only in zero(0) in , that s in z The number of times is zero Number of occurrences !
- Therefore, the most important statistics in my notes appear : Calculate which words each character appears in , And sort by the number of times , It turns out like this :
[('z', {0}), ('w', {2}), ('u', {4}), ('x', {6}), ('g', {8}), ('h', {8, 3}), ('f', {4, 5}), ('v', {5, 7}), ('s', {6, 7}), ('r', {0, 3, 4}), ('n', {1, 9, 7}), ('t', {8, 2, 3}), ('o', {0, 1, 2, 4}), ('i', {8, 9, 5, 6}), ('e', {0, 1, 3, 5, 7, 8, 9})]- Not very good. , Let me type a watch :
| Letter | Appear in several numbers | What are the specific ones |
|---|---|---|
| z | 1 | 0 |
| w | 1 | 2 |
| u | 1 | 4 |
| x | 1 | 6 |
| g | 1 | 8 |
| h | 2 | 8,3 |
| f | 2 | 4,5 |
| v | 2 | 5,7 |
| s | 2 | 6,7 |
| r | 3 | 0,3,4 |
| n | 3 | 1,9,7 |
| t | 3 | 8,2,3 |
| o | 4 | 0,1,2,4 |
| i | 4 | 8,9,5,6 |
| e | 7 | 0,1,3,5,7,8,9 |
- With this watch , We found that
z w u x g, It only appears in a certain word , Then in the string s Count the number of occurrences of these characters in , It is equal to the number of times their corresponding words appear . - this 5 The two words are
z:0,w:2,u:4,x:6,g:8. - What about the rest , Let's continue to look down on this table ,h This line ,h Only in 8 and 3 in . and 8 We can know the quantity of , be equal to g Number of occurrences , Then I just need to deal with it in advance g, hold 8 Subtract the corresponding number from all the corresponding characters ( signify s Does not exist in the 8 了 ), Equate to h Can only appear in 3 in , that h You can also find it .
- Similarly, continue to process downward , All the time v No problem . Last one left 1 and 9 Out-of-service n To represent the , Because they appear at the same time n in , Keep looking backwards , Find out i In addition to 1 It's all dealt with , Then use i To represent the 1, There's one left i To represent the 9.
- Just type this watch . The rest is coding .
- In fact, it uses Counter Even trouble , There is no preprocessing subtraction in the official solution , When updating the answer directly, subtract , Faster .
2. Complexity analysis
Worst time complexity O(n×10),n yes s The length of ,10 It's a total of 10 Word , The maximum word length is 5, Ignore this constant .
3. Code implementation
The meter .
words = [
'zero','one','two','three','four',
'five','six','seven','eight','nine'
]
cnts = [Counter(w) for w in words]
# lens = defaultdict(list)
# for w in words:
# lens[len(w)].append(w)
# c_times = defaultdict(set)
# for i,w in enumerate(words):
# for c in w:
# c_times[c].add(i)
# c_times_list = sorted(c_times.items(),key = lambda x:len(x[1]))
# print(c_times_list)
orders = [
('z',0),
('w',2),
('u',4),
('x',6),
('g',8),
('h',3),
('f',5),
('v',7),
('o',1),
('i',9)
]
class Solution:
def originalDigits(self, s: str) -> str:
cs = Counter(s)
ans = [0] * 10
for c,n in orders:
# print(cs)
ans[n] = cs[c]
for _ in range(ans[n]):
cs -= cnts[n]
# print(ans)
ret = []
for i,v in enumerate(ans):
ret.extend([i]*v)
return ''.join(map(str,ret))
3、 ... and 、 Summary of this topic
- Brain teaser problems need to be studied for a while .
边栏推荐
- UE4 shadow: perobjectshadow verification
- UE4阴影:PerObjectShadow验证
- Tableau JDBC连接GraphDB
- The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping quantile of dataframe data
- 词云图,词频图,专门统计某些关键词的词云词频
- R语言使用dplyr包的arrange函数进行dataframe排序、arrange函数基于一个字段(变量)进行降序排序实战
- Introduction to sap appgyver
- 初始 Redis(认识Redis以及常见命令)
- Mathematical modeling does not know latex typesetting | it teaches you how to use beautiful latex formulas gracefully in word
- Play through ansible directory in one step
猜你喜欢
随机推荐
R language uses LM function to build regression model and BoxCox function of mass package to find the best power transformation to improve the fitting degree of the model (determine the best λ Paramet
Sword finger offer 55 - ii balanced binary tree
剑指 Offer 55 - I. 二叉树的深度
Use of prettier code formatting tool
R language ggplot2 visualization: ggplot2 visualizes density plot and uses geom_ Vline function adds mean vertical line and mean value label (mean line or vertical line)
redis实现分布式锁
Programming implementation of I2C communication protocol
【我的OpenGL学习进阶之旅】NDK开发中find_library查找的系统动态库在哪里?
剑指 Offer 57 - II. 和为s的连续正数序列
Uniapp request request encapsulation method
Sword finger offer 54 The k-th node of binary search tree
Hystrix deployment
关于#sql#的问题:orcale sql, 为什么MERCHANT table的外键inventory—id 语句是无效的
About SQL: orcale SQL, why is the foreign key inventory ID statement of the merchant table invalid
博客从 CloudBase 迁移至云主机
[cloud native] Devops (IV): integrated sonar Qube
Brits: detailed explanation of bidirectional recursive imputation for time series
c语言做推箱子
Discussion on ble Bluetooth battery service
Sword finger offer 57 - ii Continuous positive sequence with sum s









