• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode1181.前后拼接|BeforeandAfterPuzzle

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

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

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

Given a list of phrases, generate a list of Before and After puzzles.

phrase is a string that consists of lowercase English letters and spaces only. No space appears in the start or the end of a phrase. There are no consecutive spaces in a phrase.

Before and After puzzles are phrases that are formed by merging two phrases where the last word of the first phrase is the same as the first word of the second phrase.

Return the Before and After puzzles that can be formed by every two phrases phrases[i] and phrases[j] where i != j. Note that the order of matching two phrases matters, we want to consider both orders.

You should return a list of distinct strings sorted lexicographically.

 

Example 1:

Input: phrases = ["writing code","code rocks"]
Output: ["writing code rocks"]

Example 2:

Input: phrases = ["mission statement",
                  "a quick bite to eat",
                  "a chip off the old block",
                  "chocolate bar",
                  "mission impossible",
                  "a man on a mission",
                  "block party",
                  "eat my words",
                  "bar of soap"]
Output: ["a chip off the old block party",
         "a man on a mission impossible",
         "a man on a mission statement",
         "a quick bite to eat my words",
         "chocolate bar of soap"]

Example 3:

Input: phrases = ["a","b","a"]
Output: ["a"]

 

Constraints:

  • 1 <= phrases.length <= 100
  • 1 <= phrases[i].length <= 100

 

给你一个「短语」列表 phrases,请你帮忙按规则生成拼接后的「新短语」列表。

「短语」(phrase)是仅由小写英文字母和空格组成的字符串。「短语」的开头和结尾都不会出现空格,「短语」中的空格不会连续出现。

「前后拼接」(Before and After puzzles)是合并两个「短语」形成「新短语」的方法。我们规定拼接时,第一个短语的最后一个单词 和 第二个短语的第一个单词 必须相同。

返回每两个「短语」 phrases[i] 和 phrases[j]i != j)进行「前后拼接」得到的「新短语」。

注意,两个「短语」拼接时的顺序也很重要,我们需要同时考虑这两个「短语」。另外,同一个「短语」可以多次参与拼接,但「新短语」不能再参与拼接。

请你按字典序排列并返回「新短语」列表,列表中的字符串应该是 不重复的 。

 

示例 1:

输入:phrases = ["writing code","code rocks"]
输出:["writing code rocks"]

示例 2:

输入:phrases = ["mission statement",
                "a quick bite to eat",
                "a chip off the old block",
                "chocolate bar",
                "mission impossible",
                "a man on a mission",
                "block party",
                "eat my words",
                "bar of soap"]
输出:["a chip off the old block party",
      "a man on a mission impossible",
      "a man on a mission statement",
      "a quick bite to eat my words",
      "chocolate bar of soap"]

示例 3:

输入:phrases = ["a","b","a"]
输出:["a"]

 

提示:

  • 1 <= phrases.length <= 100
  • 1 <= phrases[i].length <= 100

Runtime: 112 ms
Memory Usage: 23.5 MB
 1 class Solution {
 2     func beforeAndAfterPuzzles(_ phrases: [String]) -> [String] {
 3         var map:[String:[Int]] = [String:[Int]]()
 4         var i:Int = 0
 5         for str in phrases
 6         {
 7             let first:String = str.components(separatedBy:" ").first!
 8             map[first,default:[Int]()].append(i)
 9             i += 1
10         }
11         i = 0
12         var res:Set<String> = Set<String>()
13         for str in phrases
14         {
15             let array:[String] = str.components(separatedBy:" ")
16             let last = array.last!
17             if map[last] != nil
18             {
19                 let arr:[Int] = map[last]!
20                 for index in arr
21                 {
22                     if index == i {continue}
23                     res.insert(str + phrases[index].subString(last.count))
24                 }
25             }
26             i += 1
27         }
28         return  Array(res).sorted(by:<)
29     }
30 }
31 
32 extension String {
33     // 截取字符串:从index到结束处
34     // - Parameter index: 开始索引
35     // - Returns: 子字符串
36     func subString(_ index: Int) -> String {
37         let theIndex = self.index(self.endIndex, offsetBy: index - self.count)
38         return String(self[theIndex..<endIndex])
39     }
40 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【原】iOS学习之Swift之语法1(精简版)发布时间:2022-07-13
下一篇:
Swift-用CATransform3DMakeRotation实现翻页效果发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap