当前位置:网站首页>leetcode:648. Word replacement [dictionary tree board + find the shortest matching prefix among several prefixes]
leetcode:648. Word replacement [dictionary tree board + find the shortest matching prefix among several prefixes]
2022-07-07 14:39:00 【White speed Dragon King's review】
analysis
To a Trie Record dictionary tree
Then traverse word by word , use cur Go to the current layer
If you traverse to a c, Not in the cur in , Set up cur[c] It's empty , cur = cur[c]
At the end, let's say # It means the ending
Then traverse sentence Every word in , If you traverse to # Description matches a prefix , Prefix replacement
If you find out that c be not in cur in , It means that there is no match , direct break
ac code
class Solution:
def replaceWords(self, dictionary: List[str], sentence: str) -> str:
# Dictionary tree board
trie = {
}
for word in dictionary:
cur = trie
for c in word:
# New branch
if c not in cur:
cur[c] = {
}
cur = cur[c]
# End mark
cur['#'] = {
}
words = sentence.split()
for i in range(len(words)):
# stay trie Find the shortest prefix
cur = trie
for j, c in enumerate(words[i]):
# Match to the first prefix
if '#' in cur:
words[i] = words[i][:j]
break
# Matching failure
if c not in cur:
break
# Continue matching
cur = cur[c]
#print(words)
return ' '.join(words)
summary
Dictionary tree board
You can find the shortest matching prefix among several prefixes
边栏推荐
- Leetcode——剑指 Offer 05. 替换空格
- 数据湖(九):Iceberg特点详述和数据类型
- Instructions d'utilisation de la trousse de développement du module d'acquisition d'accord du testeur mictr01
- Assign a dynamic value to the background color of DataGrid through ivalueconverter
- Data connection mode in low code platform (Part 2)
- 6、Electron无边框窗口和透明窗口 锁定模式 设置窗口图标
- Simple steps for modifying IP of sigang electronic scale
- Data Lake (IX): Iceberg features and data types
- C # use TCP protocol to establish connection
- 因员工将密码设为“123456”,AMD 被盗 450Gb 数据?
猜你喜欢
2022pagc Golden Sail award | rongyun won the "outstanding product technology service provider of the year"
大厂做开源的五大痛点
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条...
Huawei cloud database DDS products are deeply enabled
Substance painter notes: settings for multi display and multi-resolution displays
设备故障预测机床故障提前预警机械设备振动监测机床故障预警CNC震动无线监控设备异常提前预警
小程序目录结构
Data Lake (IX): Iceberg features and data types
Navigation — 这么好用的导航框架你确定不来看看?
What is cloud primordial? This time, I can finally understand!
随机推荐
课设之百万数据文档存取
全球首款 RISC-V 笔记本电脑开启预售,专为元宇宙而生!
Oracle Linux 9.0 正式发布
Pandora IOT development board learning (HAL Library) - Experiment 12 RTC real-time clock experiment (learning notes)
Es log error appreciation -trying to create too many buckets
Hangdian oj2092 integer solution
Use case diagram
Demis Hassabis谈AlphaFold未来目标
EfficientNet模型的完整细节
低代码平台中的数据连接方式(下)
Search engine interface
Bill Gates posted his resume 48 years ago: "it's not as good-looking as yours."
【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
Computer win7 system desktop icon is too large, how to turn it down
Navigation — 这么好用的导航框架你确定不来看看?
WebRTC 音频抗弱网技术(上)
JS image to Base64
Navigation - are you sure you want to take a look at such an easy-to-use navigation framework?
NDK beginner's study (1)
Leetcode - Sword finger offer 05 Replace spaces