720. Longest Word in Dictionary 能连续拼接出来的最长单词
[抄题]:
Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicographical order.
If there is no answer, return the empty string.
Example 1:
Input:
words = ["w","wo","wor","worl", "world"]
Output: "world"
Explanation:
The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
Example 2:
Input:
words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
Output: "apple"
Explanation:
Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
添加单词时注意:“长度是1”的往往比“初始为0”包含的case更多 如 c ca cat, m, mo, moc, moch, mocha。所以一般还是写“长度是1”
[思维问题]:
[一句话思路]:
单词前缀都一样的,可以用trie树,也可以用hashset,表示曾经出现过
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
所以一般还是写“长度是1”
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
不是想用hashset,而是数组根本就不能很方便地用.contains()判断存在性
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public String longestWord(String[] words) {
//ini: set,res = "", sort
Set set = new HashSet();
String res = "";
Arrays.sort(words);
//cc
if (words == null) {
return res;
}
//for loop : set.add(w), res ?= w
for (int i = 0; i < words.length; i++) {
if (words[i].length() == 1 || set.contains(words[i].substring(0, words[i].length() - 1))) {
res = (words[i].length() > res.length()) ? words[i] : res;
set.add(words[i]);
}
}
//return
return res;
}
}
720. Longest Word in Dictionary 能连续拼接出来的最长单词的更多相关文章
- 【Leetcode_easy】720. Longest Word in Dictionary
problem 720. Longest Word in Dictionary 题意: solution1: BFS; class Solution { public: string longestW ...
- [LeetCode&Python] Problem 720. Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- leetcode 720. Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- LeetCode 720. Longest Word in Dictionary (字典里最长的单词)
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...
- 720. Longest Word in Dictionary
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- [leetcode]720. Longest Word in Dictionary字典中最长的单词
b.compareTo(a) 这个函数是比较两个值得大小,如果b比a大,那么返回1 如果小,那么返回-1,相等返回0 如果比较的是字符串,那么比较字典编纂顺序,b靠前返回-1,靠后返回1 这个题的核心 ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
随机推荐
- 伪元素:focus-within
1.盒子边框的线条动画: <div class="cont"> <div class="bb"></div> </di ...
- 使用python对文件中的数值进行累加
问题描述: 一个文件由若干条记录组成,记录的格式为:“num1 num2”,有时候,需要统计文件中num1对应的num2的总值.处理问题的思路 用传说中的python来处理,很方便.几行代码就可以了. ...
- HDU - 6098:Inversion(暴力均摊)
Give an array A, the index starts from 1. Now we want to know B i =max i∤j A j Bi=maxi∤jAj , i≥2 i≥ ...
- 模仿Masonry链式编程思想
使用masonry 也将近一年多了,它的链式编程方式一直是很吸引我的. 之前一直没空好好思考它是如何实现,直到现在正好自己有空,因此写下链式编程的基本思路. 链式基本的编程形式如 a.property ...
- Servlet和JSP规范与Tomcat版本对应关系
apache tomcat 官网地址:http://tomcat.apache.org/whichversion.html
- 阿里云ESC服务器安装tomcat后无法远程访问
问题描述:服务器上面没有部署文件,安装了tomcat,在服务器本地能通过"localhost:8080"访问到tom猫页面 但是远程访问“外网ip+:8080”就访问不了 解决方案 ...
- gitlab Failed to register this runner. Perhaps you are having network problems runner 注册失败问题解决
1. 低版本安装地址 https://gitlab-ci-multi-runner-downloads.s3.amazonaws.com/v1.11.2/index.html 2. 使用 yum ...
- hdu 4609 3-idiots——FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4609 答案就是随便选三条边的方案 - 不合法的方案. 不合法的方案就是算出 x+y = k 的方案数 g[ ...
- 洛谷3004 [USACO10DEC]宝箱Treasure Chest
题目:https://www.luogu.org/problemnew/show/P3004 一眼看上去就是记忆化搜索的dp.像 一双木棋 一样. 结果忘了记忆化.T了5个点. 然后加上记忆化.MLE ...
- (转)Android开发--常用的传感器总结
随着手机的发展,现在各大手机支持的传感器类型也越来越多,在开发中利用传感器进行某些操作令人们有一种耳目一新的感觉,例如微信中的摇一摇,以及手机音乐播放器中的摇一摇切歌.今天来简单介绍下Android中 ...