[Swift]LeetCode1181. 先後拼接 | Before and After Puzzle

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(WeiGanTechnologies)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-onjfufki-hr.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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.github

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.app

You should return a list of distinct strings sorted lexicographically.ide

 

Example 1:ui

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

Example 2:spa

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:code

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

 

Constraints:component

  • 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 }
相關文章
相關標籤/搜索