[Swift]LeetCode472. 连接词 | Concatenated Words

[Swift]LeetCode472. 连接词 | Concatenated Words,第1张

概述Given a list of words (without duplicates), 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

Given a List of words (without duplicates),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","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. All the input string will only include lower case letters. The returned elements order does not matter.

给定一个不含重复单词的列表,编写一个程序,返回给定单词列表中所有的连接词

连接词的定义为:一个字符串完全是由至少两个给定数组中的单词组成的。

示例:

输入: ["cat","ratcatdogcat"]输出: ["catsdogcats","ratcatdogcat"]解释: "catsdogcats"由"cats","dog" 和 "cats"组成;      "dogcatsdog"由"dog","cats"和"dog"组成;      "ratcatdogcat"由"rat","dog"和"cat"组成。

说明:

给定数组的元素总数不超过 10000。 给定数组中元素的长度总和不超过 600000。 所有输入字符串只包含小写字母。 不需要考虑答案输出的顺序。 Runtime: 8332 ms Memory Usage: 10.2 MB
 1 class Solution { 2     func findAllConcatenateDWordsInADict(_ words: [String]) -> [String] { 3         var res:[String] = [String]() 4         var dict:Set<String> = Set(words) 5         for word in words 6         { 7             var n:Int = word.count 8             if n == 0 {continue} 9             var dp:[Bool] = [Bool](repeating:false,count:n + 1)10             dp[0] = true11             for i in 0..<n12             {13                 if !dp[i] {continue}14                 for j in (i + 1)...n15                 {16                     var str:String = word.subString(i,j - i)17                     if j - i < n && dict.contains(str)18                     {19                         dp[j] = true20                     }21                 }22                 if dp[n] 23                 {24                     res.append(word)25                     break26                 }27             }28         }29         return res30     }31 }32 33 extension String {34     // 截取字符串:指定索引和字符数35     // - begin: 开始截取处索引36     // - count: 截取的字符数量37     func subString(_ begin:Int,_ count:Int) -> String {38         let start = self.index(self.startIndex,offsetBy: max(0,begin))39         let end = self.index(self.startIndex,offsetBy:  min(self.count,begin + count))40         return String(self[start..<end]) 41     }42 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode472. 连接词 | Concatenated Words全部内容,希望文章能够帮你解决[Swift]LeetCode472. 连接词 | Concatenated Words所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1020021.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存