Hash Table-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".

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].
class Solution {
 2 public:
 3     string longestWord(vector<string>& words) {
 4         sort(words.begin(),words.end()) ;
 5         unordered_set<string> Set;
 6         string res = "" ;
 7         for(string w : words){
 8             if (w.length() == 1 || Set.count(w.substr(0,w.length()-1))){
 9                 res = w.length() > res.length() ? w : res ;
10                 Set.insert(w) ;
11             }
12         }
13         return res ;
14     }
15 };

原文地址:https://www.cnblogs.com/msymm/p/8253793.html

时间: 2024-08-22 21:14:28

Hash Table-720. Longest Word in Dictionary的相关文章

leetcode 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 l

[leetcode]720. Longest Word in Dictionary字典中最长的单词

b.compareTo(a) 这个函数是比较两个值得大小,如果b比a大,那么返回1 如果小,那么返回-1,相等返回0 如果比较的是字符串,那么比较字典编纂顺序,b靠前返回-1,靠后返回1 这个题的核心虽然是hashtable,但是这个方法还是很重要的,因为自己实现比较字符串很麻烦 /* 用一个Hashset来判断子串是不是在集合里 然后遍历出最长的就行 长度相同的用compareTo方法判断谁的字典顺序靠前 这个题最难的感觉其实是不知道compareTo方法,自己实现还挺麻烦的 */ publi

[LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词

Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographic

[LeetCode] 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 l

[LeetCode] 524. Longest Word in Dictionary through Deleting

Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographic

524. Longest Word in Dictionary through Deleting

https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/#/solutions An alternate, more efficient solution which avoids sorting the dictionary: public String findLongestWord(String s, List<String> d) { String longest = "";

Hash table and Python dictionary

One of the most useful Python collections is the dictionary, which is an associative data type where you can store key-data pairs. It is implemented using hash tables. Hash table is a collection of items which are stored in such a way as to make it e

【string】hash table, two pointers, string

利用hash table, two pointers, string的题目. 1.求最长不重复子串的长度 hash table体现在一个数组,下标是字符串中元素的ASCII值,下标对应的元素代表该元素在字符串中出现的位置. two pointers体现在用i一步步向前移去遍历字符串中的元素,作为不重复子串的末尾位置:用j指向不重复字符区间的首字符的位置. 1 /*************************** 2 @date 4.23 3 @description https://leet

Java 散列表 hash table

Java 散列表 hash table @author ixenos hash table, HashTable, HashMap, HashSet hash table 是一种数据结构 hash table 为每个对象计算一个整数,该整数被称为散列码 hash code hash code 是由对象的实例域产生的一个整数,具有不同的数据域的对象将产生不同的hash code 如果自定义类,就要负责实现这个类的hashCode方法,注意要与equals方法兼容,即如果a.equals(b)为tr