LeetCode题解(1065):字符串的索引对(Python)
发布日期:2021-06-29 20:13:28 浏览次数:3 分类:技术文章

本文共 776 字,大约阅读时间需要 2 分钟。

题目:(简单)

标签:字符串、字典树

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( W + T × L ) O(W+T×L) O(W+T×L) O ( W + T ) O(W+T) O(W+T) 48ms (79.59%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:    def indexPairs(self, text: str, words: List[str]) -> List[List[int]]:        tree = {
} for i, word in enumerate(words): node = tree for ch in word: if ch not in node: node[ch] = {
} node = node[ch] node["@"] = i ans = [] for i in range(len(text)): node = tree j = i while j < len(text) and text[j] in node: node = node[text[j]] if "@" in node: ans.append([i, j]) j += 1 return ans

转载地址:https://dataartist.blog.csdn.net/article/details/110730744 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(1085):最小元素各数位之和(Python)
下一篇:LeetCode题解(1064):不动点(Python)

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年05月02日 10时54分19秒