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].

 

找出数组里最长的字符串,该字符串的子集也要在这个数组里,有多个答案则输出字典序最小的

 

C++(52ms):

 1 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 };

 

Java(36ms):

 1 class Solution {
 2     public String longestWord(String[] words) {
 3         Arrays.sort(words);
 4         Set<String> set = new HashSet<String>();
 5         String res = "";
 6         for (String w : words) {
 7             if (w.length() == 1 || set.contains(w.substring(0, w.length() - 1))) {
 8                 res = w.length() > res.length() ? w : res;
 9                 set.add(w);
10             }
11         }
12         return res;
13     }
14 }

 

转载于:https://www.cnblogs.com/mengchunchen/p/7927754.html

老师教学上字典和元组是这样的“在需要使⽤序列的情况下: • 函数的返回值 • 字典的键值 • 函数的参数 directory[last, first] = number • for last, first in directory: • print(first, last, directory[last,first]) def has_match(t1, t2): • for x, y in zip(t1, t2): • if x == y: • return True • return False • for index, element in enumerate('abc'): • print(index, element)请你基于老师教学的代码完成练习12-2 “Exercise 12-2. More anagrams! 148 | Chapter 12: Tuples 1. Write a program that reads a word list from a file (see "Reading Word Lists" on page 99) and prints all the sets of words that are anagrams. Here is an example of what the output might look like: ['deltas', 'desalt', 'lasted', 'salted', 'slated', 'staled'] ['retainers' ['renerating greatening , 'greatening'] ['resmelts' , 'smelters' , 'termless'] Hint: you might want to build a dictionary that maps from a collection of letters to a list of words that can be spelled with those letters. The question is, how can you represent the collection of letters in a way that can be used as a key? 2. Modify the previous program so that it prints the longest list of anagrams first, followed by the second longest, and so on. 3. In Scrabble, a "bingo" is when you play all seven tiles in your rack, along with a letter on the board, to form an eight-letter word. What collection of eight letters forms the most possible bingos? Hint: there are seven.”请你运用python运行规则 编写练习12-2要求的运行代码 注意代码编写具有可读性和完整性 能够真实运行出符合要求的数据 注意分两版答案 第一版应该有注释 第二版为纯代码 其中题目有说明解题的思路 可以按照题目的思路步骤一步一步完成
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值