Created
August 12, 2018 10:24
-
-
Save luojiyin1987/5215693b0dfe32605c7ba527b6a23a94 to your computer and use it in GitHub Desktop.
Longest Word in Dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution(object): | |
| def longestWord(self, words): | |
| """ | |
| :type words: List[str] | |
| :rtype: str | |
| """ | |
| wSet =set(['']) | |
| ans = '' | |
| for word in sorted(words): | |
| if word[:-1] in wSet: | |
| wSet.add(word) | |
| if len(word) > len(ans): | |
| ans = word | |
| return ans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment