当前位置:网站首页>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
边栏推荐
- 杭电oj2054 A == B ? ???
- Emqx 5.0 release: open source Internet of things message server with single cluster supporting 100million mqtt connections
- 6. Electron borderless window and transparent window lock mode setting window icon
- UML state diagram
- STM32CubeMX,68套组件,遵循10条开源协议
- Analysis of arouter
- Huawei cloud database DDS products are deeply enabled
- Demis Hassabis谈AlphaFold未来目标
- The longest ascending subsequence model acwing 482 Chorus formation
- Million data document access of course design
猜你喜欢
GAN发明者Ian Goodfellow正式加入DeepMind,任Research Scientist
设备故障预测机床故障提前预警机械设备振动监测机床故障预警CNC震动无线监控设备异常提前预警
AWS学习笔记(三)
Pandora IOT development board learning (HAL Library) - Experiment 12 RTC real-time clock experiment (learning notes)
在软件工程领域,搞科研的这十年!
Bill Gates posted his resume 48 years ago: "it's not as good-looking as yours."
JS get the current time, month, day, year, and the uniapp location applet opens the map to select the location
Full details of efficientnet model
Notes de l'imprimante substance: paramètres pour les affichages Multi - écrans et multi - Résolutions
What is cloud primordial? This time, I can finally understand!
随机推荐
Electronic remote error
Simple use of websocket
Instructions for mictr01 tester vibrating string acquisition module development kit
Oracle Linux 9.0 officially released
Mrs offline data analysis: process OBS data through Flink job
Instructions d'utilisation de la trousse de développement du module d'acquisition d'accord du testeur mictr01
Oracle non automatic submission solution
比尔·盖茨晒48年前简历:“没你们的好看”
Leetcode - Sword finger offer 05 Replace spaces
bashrc与profile
关于后台动态模板添加内容的总结 Builder使用
Because the employee set the password to "123456", amd stolen 450gb data?
Leetcode——236. The nearest common ancestor of binary tree
leetcode:648. 单词替换【字典树板子 + 寻找若干前缀中的最短符合前缀】
The longest ascending subsequence model acwing 1014 Mountaineering
Substance Painter筆記:多顯示器且多分辨率顯示器時的設置
【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
Small game design framework
「2022年7月」WuKong编辑器更版记录
今日睡眠质量记录78分