zoukankan      html  css  js  c++  java
  • [Swift]LeetCode1181. 前后拼接 | Before and After Puzzle

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

    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 }
  • 相关阅读:
    黑客网银木马服务器曝光 14家银行网银遭监控 狼人:
    卡巴斯基实验室CE0来华启动卡巴斯基安全中国行 狼人:
    天清汉马UTM获“北京市自主创新产品”称号 狼人:
    IBM称欧亚受Conficker病毒感染最严重 狼人:
    卡巴斯基联手功夫巨星成龙 五月鸟巢开唱 狼人:
    微软4月14日起不再为所有XP用户提供安全补丁 狼人:
    卡巴斯基爱好者见面会 卡巴斯基先生与卡fans亲密互动 狼人:
    愚人节黑客以身试法 人民法院被挂马 狼人:
    微软:97%电子邮件属于垃圾邮件 狼人:
    4月3日 尤金.卡巴斯基在北大精彩演讲 狼人:
  • 原文地址:https://www.cnblogs.com/strengthen/p/11484245.html
Copyright © 2011-2022 走看看