[Swift] LeetCode1181 before and after stitching |. Before and After Puzzle

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: to dare (WeiGanTechnologies)
➤ blog Park address: San-ching Wing Chi ( https://www.cnblogs.com/strengthen/ )
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address: HTTPS: //www.cnblogs. com / strengthen / p / 11484245.html
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

 

To give you a "phrase" list  phrases, please help by the rules to generate "a new phrase" list after stitching.

"Phrase" (phrase) is a string of only lowercase letters and spaces. "The phrase" the beginning and end of the space will not occur, "the phrase" the spaces are not consecutive.

"Before and after stitching" (Before and After puzzles) is to merge the two, "the phrase" method "new phrase" formation. When we require stitching, the last word of the first phrase and the second phrase of the first word must be the same.

Return every two 'phrase'  phrases[i] and  phrases[j]( i != jconduct "before and after splicing" get "new phrase").

Note that the order of two "phrase" stitching is also very important, we need to consider both "phrase." In addition, the same "phrase" may be repeatedly involved in splicing, but "a new phrase" can no longer participate in splicing.

Please arranged in sequence and returns the dictionary "new phrase" list, the list of strings should not be repeated.

 

Example 1:

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

Example 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"]

Example 3:

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

 

prompt:

  • 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 is              I = + . 1 
27          }
 28          return   the Array (RES) .sorted (by: < )
 29      }
 30  }
 31 is  
32  Extension String {
 33      // taken string: from the end of the index
 34 is      // - the Parameter index: start index
 35      @ - Returns: substring 
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 }

 

Guess you like

Origin www.cnblogs.com/strengthen/p/11484245.html