720. Longest Word in Dictionary
来源:互联网 发布:怎么建数据库 编辑:程序博客网 时间:2024/06/11 08:51
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".
Note:
All the strings in the input will only contain lowercase letters.
The length of words will be in the range [1, 1000].
The length of words[i] will be in the range [1, 30].
思路:
给定字符串数组,找出包含最长的字符串(包含其他单词的字母),如果存在相同长度,取字母排序小的
对数组排序,再利用Set对字母存储,小的单词一定包含在后面大的单词里面。后面只需要取前缀相同的
对字母排序后,第一个单词一定是共有的,后面只需在此基础上添加
如果单词只有一个字母,那一定是共有的
class Solution { public String longestWord(String[] words) { Arrays.sort(words); HashSet<String> set=new HashSet<String>(); String res=""; for(String s:words){ if(s.length()==1||set.contains(s.substring(0,s.length()-1))){ res=s.length()>res.length()?s:res; set.add(s); } } return res; }}
阅读全文
0 0
- 720. Longest Word in Dictionary
- LeetCode 720. Longest Word In Dictionary
- LWC 57:720. Longest Word in Dictionary
- leetcode 720. Longest Word in Dictionary
- Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- leetcode524~Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- [LeetCode] Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- glassfish基本使用
- golang操作memcached
- linux下C程序的编译链接
- 用两个线程玩猜数字游戏,第一个线程负责随机给出1~100之间的一个整数,第二个线程负责猜出这个数。要求每当第二个线程给出自己的猜测后,第一个线程都会提示“猜小了”、“猜大了”或“猜对
- 转载 Java 代码性能优化总结
- 720. Longest Word in Dictionary
- react文档阅读笔记----JSX属性
- 码的, hibernate, 添加新表后,得到 表的 主键ID
- Jackson之jackson-core
- LCD
- hdu 5459(递推+步步取模)
- 乔姆斯基4种文法的判定和区别
- OpenResty 最佳实践学习--实战演习笔记(5)
- KMP