LeetCode 472. Concatenated Words
来源:互联网 发布:网络舆论的传播途径 编辑:程序博客网 时间:2024/04/28 22:08
Problem Statement
(Source) Given a list of words, please write a program that returns all concatenated words in the given list of words.
A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.
Example:
Input: ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats"; "dogcatsdog" can be concatenated by "dog", "cats" and "dog"; "ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".
Note:
- The number of elements of the given array will not exceed
10,000
- The length sum of elements in the given array will not exceed
600,000
. - The returned elements order does not matter.
Solution
class Solution(object): def findAllConcatenatedWordsInADict(self, words): """ :type words: List[str] :rtype: List[str] """ st = set(words) def helper(word): sta = [0] explored = {0} n = len(word) while sta: left = sta.pop() if left == n: return True for right in xrange(left+1, n+1): if word[left : right] in st and right not in explored and (right != n or left > 0): sta.append(right) explored.add(right) return False return [word for word in words if word and helper(word)]
0 0
- LeetCode 472. Concatenated Words
- leetcode-472. Concatenated Words
- Leetcode 472. Concatenated Words
- [leetcode]472. Concatenated Words
- LeetCode 472. Concatenated Words
- leetcode 472. Concatenated Words
- [Leetcode] 472. Concatenated Words 解题报告
- [LeetCode]Concatenated Words
- LeetCode【472】 Concatenated Words
- 472. Concatenated Words
- 472. Concatenated Words
- Leetcode 472 - Concatenated Words(dp)
- Concatenated Words
- leetcode 472. Concatenated Words 连接字符串的判定 + 动态规划DP实现
- [Leetcode] 555. Split Concatenated Strings 解题报告
- Leetcode - Reverse Words
- Leetcode--648. Replace Words
- leetcode 648. Replace Words
- eclipse中Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
- 如何才能写出软文有分量的标题
- seo优化我们常常需要思考的几个点
- 如何将PDF转为word
- 现如今seo优化的趋势
- LeetCode 472. Concatenated Words
- MySQL 隔离级别
- 设计模式-工厂方法模式
- File类的用法
- GDI学习旅途基础篇之一
- Socket编程 服务端 客户端工作方法
- CCSIExternalTransformerExecutor和 CCSIExternalTransformerExecutor::Impl
- C语言- 猜测数字小游戏
- HDFS Commands Guide