题目如下:

Given many wordswords[i] has weight i.

Design a class WordFilter that supports one function, WordFilter.f(String prefix, String suffix). It will return the word with given prefix and suffix with maximum weight. If no word exists, return -1.

Examples:

Input:
WordFilter(["apple"])
WordFilter.f("a", "e") // returns 0
WordFilter.f("b", "") // returns -1

Note:

  1. words has length in range [1, 15000].
  2. For each test case, up to words.length queries WordFilter.f may be made.
  3. words[i] has length in range [1, 10].
  4. prefix, suffix have lengths in range [0, 10].
  5. words[i] and prefix, suffix queries consist of lowercase letters only.

解题思路:以输入apple为例,前缀和后缀分别有6个,分别为"","a","ap","app","appl","apple",两者组合起来就是36个。words中最长的word的长度是10,那么组合就是11*11 = 121个,words的最大长度是15000,总的组合个数1815000,似乎在可以接受的范围之内。所以,只要预先把所有单词的所有前缀后缀的组合预先计算出来,那么查找的时间复杂度就是O(1)了。

代码如下:

class WordFilter(object):

    def __init__(self, words):
"""
:type words: List[str]
"""
self.dic = {}
for i in range(len(words)-1,-1,-1):
word = words[i]
foward = ''
reverse = word
while len(reverse) >= 0:
if (foward,reverse) not in self.dic:
self.dic[(foward,reverse)] = i
for j in range(len(word)):
foward += word[j]
if (foward,reverse) not in self.dic:
self.dic[(foward,reverse)] = i
if len(reverse) > 0:reverse = reverse[1:]
else:break
foward = '' def f(self, prefix, suffix):
"""
:type prefix: str
:type suffix: str
:rtype: int
"""
if (prefix,suffix) in self.dic:
return self.dic[(prefix,suffix)]
return -1

【leetcode】745. Prefix and Suffix Search的更多相关文章

  1. 745. Prefix and Suffix Search 查找最大index的单词

    [抄题]: Given many words, words[i] has weight i. Design a class WordFilter that supports one function, ...

  2. 【LeetCode】669. Trim a Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  3. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  4. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  5. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  6. 【LeetCode】前缀树 trie(共14题)

    [208]Implement Trie (Prefix Tree) (2018年11月27日) 实现基本的 trie 树,包括 insert, search, startWith 操作等 api. 题 ...

  7. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  8. 【LeetCode】树(共94题)

    [94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...

  9. 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)

    [LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...

随机推荐

  1. 【图像-视频处理】YUV420、YV12与RGB24的转换公式

    bool YV12ToBGR24_Native(unsigned char* pYUV,unsigned char* pBGR24,int width,int height) { if (width ...

  2. 给Date的构造函数添加属性和方法

    let d = Date.prototype; Object.defineProperties(d, { 'year': { get: function () { return this.getFul ...

  3. HanLP-最短路径分词

    今天介绍的内容是最短路径分词.最近换回了thinkpad x1,原因是mac的13.3寸的屏幕看代码实在是不方便,也可能是人老了吧,^_^.等把HanLP词法分析介绍结束后,还是会换回macbook ...

  4. iframe高度/宽度自适应(使用body而不是docuemntElement对象)

    iframe在ie11中会显示过于短.为了自适应,增加如下代码: <iframe *** onload='changeFrameHeight()' > <script> fun ...

  5. 微信小程序打开地图选择位置

    wx.getLocation({ type: 'wgs84', success(res) { const latitude = res.latitude const longitude = res.l ...

  6. Intel Driver and Support Assistant 安装失败

    Intel Driver and Support Assistant 以下简称 Intel DSA. Intel DSA 依赖 Microsoft Visual C++ 2015-2019 Redis ...

  7. UML表示类图和对象图

    类图表示不同的实体(人.事物和数据)如何彼此相关,显示了系统的静态结构.类图可用于表示逻辑类,逻辑类通常就是业务人员所谈及的事物种类,比如摇滚乐队.CD.广播剧,或者贷款.住房抵押.汽车信贷及利率的抽 ...

  8. linux 使用tmux

    一. 什么是tmux 1.1. tmux 是两个单词的缩写,即“Terminal MultipleXer”,意思是“终端复用器“ 1.2. tmux 结构 1.2.1. tmux主要由三层: < ...

  9. FFmpeg4.0笔记:采集系统声音

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff // 采集系统声音 void test_systemsound() ...

  10. spark教程(六)-Python 编程与 spark-submit 命令

    hadoop 是 java 开发的,原生支持 java:spark 是 scala 开发的,原生支持 scala: spark 还支持 java.python.R,本文只介绍 python spark ...