换新天。为有牺牲多壮志,敢教日月

[Swift]LeetCode472. 连接词 | Concatenated Words

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10347611.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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","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:

  1. The number of elements of the given array will not exceed 10,000
  2. The length sum of elements in the given array will not exceed 600,000.
  3. All the input string will only include lower case letters.
  4. The returned elements order does not matter.

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

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

示例:

输入: ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]

输出: ["catsdogcats","dogcatsdog","ratcatdogcat"]

解释: "catsdogcats"由"cats", "dog" 和 "cats"组成; 
     "dogcatsdog"由"dog", "cats"和"dog"组成; 
     "ratcatdogcat"由"rat", "cat", "dog"和"cat"组成。

说明:

  1. 给定数组的元素总数不超过 10000
  2. 给定数组中元素的长度总和不超过 600000
  3. 所有输入字符串只包含小写字母。
  4. 不需要考虑答案输出的顺序。

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] = true
11             for i in 0..<n
12             {
13                 if !dp[i] {continue}
14                 for j in (i + 1)...n
15                 {
16                     var str:String = word.subString(i, j - i)
17                     if j - i < n && dict.contains(str)
18                     {
19                         dp[j] = true
20                     }
21                 }
22                 if dp[n] 
23                 {
24                     res.append(word)
25                     break
26                 }
27             }
28         }
29         return res
30     }
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 }
复制代码

 

posted @   为敢技术  阅读(320)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°