面试17.13恢复空格

   日期:2024-01-17     浏览:46    评论:0    
# 动态规划的解法,参考别人的。
from typing import List
class Solution:
def respace(self, dictionary: List[str], sentence: str) -> int:
d = {}.fromkeys(dictionary,"heheh")
length = len(sentence)
f = [0] * (length + 1)
for index in range(1,length + 1):
f[index] = f[index - 1] + 1
for index1 in range(index):
if sentence[index1:index] in d:
f[index] = min(f[index],f[index1])
print(f)
return f[-1]
A = Solution()
# print(A.respace(["looked","just","like","her","brother"],"jesslookedjustliketimherbrother"))
print(A.respace(["aaysaayayaasyya","yyas","yayysaaayasasssy","yaasassssssayaassyaayaayaasssasysssaaayysaaasaysyaasaaaaaasayaayayysasaaaa","aya","sya","ysasasy","syaaaa","aaaas","ysa","a","aasyaaassyaayaayaasyayaa","ssaayayyssyaayyysyayaasaaa","aya","aaasaay","aaaa","ayyyayssaasasysaasaaayassasysaaayaassyysyaysaayyasayaaysyyaasasasaayyasasyaaaasysasy","aaasa","ysayssyasyyaaasyaaaayaaaaaaaaassaaa","aasayaaaayssayyaayaaaaayaaays","s"],"asasayaayaassayyayyyyssyaassasaysaaysaayaaaaysyaaaa"))
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服