[Swift]LeetCode269. 外星人詞典 $ Alien Dictionary

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

There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.git

Example 1:github

Input:
[
  "wrt",
  "wrf",
  "er",
  "ett",
  "rftt"
]

Output: 
"wertf"

Example 2:數組

Input:
[
  "z",
  "x"
]

Output: 
"zx"

Example 3:微信

Input:
[
  "z",
  "x",
  "z"
] 

Output:  

Explanation: The order is invalid, so return .
""""

Note:app

  1. You may assume all letters are in lowercase.
  2. You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
  3. If the order is invalid, return an empty string.
  4. There may be multiple valid order of letters, return any one of them is fine.

有一種新的外來語言使用拉丁字母。 可是,您不知道字母之間的順序。 您會從字典中收到一個非空單詞列表,其中的單詞按照新語言的規則按字典順序排序。 導出這種語言的字母順序。函數

例1:測試

Input:
[
  "wrt",
  "wrf",
  "er",
  "ett",
  "rftt"
]

Output: "wertf"

例2:this

Input:
[
  "z",
  "x"
]

Output: "zx"

例3:spa

Input:
[
  "z",
  "x",
  "z"
] 

Output:  ""

說明:訂單無效,所以請返回「」。
注意:

一、您能夠假設全部字母都是小寫的。
二、您能夠假設若是a是b的前綴,則a必須出如今給定字典中的b以前。
三、若是訂單無效,則返回空字符串。
四、可能有多個有效的字母順序,返回其中任何一個都沒問題。


Solution

 1 class Solution {
 2     func alienOrder(_ words: [String]) -> String {
 3         var st:Set<[Character]> = Set<[Character]>()
 4         var ch:Set<Character> = Set<Character>()
 5         var ins:[Int] = [Int](repeating:0,count:256)
 6         var q:[Character] = [Character]()
 7         var res:String = String()
 8         for a in words
 9         {
10             for c in a
11             {
12                 ch.insert(c)
13             }
14         }
15         for i in 0..<words.count - 1
16         {
17             let mn:Int = min(words[i].count, words[i + 1].count)
18             var j:Int = 0
19             while(j < min(words[i].count, words[i + 1].count))
20             {
21                 if words[i][j] != words[i + 1][j]
22                 {
23                     st.insert([words[i][j], words[i + 1][j]])
24                     break
25                 }
26                 j += 1
27             }
28             if j == mn && words[i].count > words[i + 1].count
29             {
30                 return String()
31             }
32         }
33         for a in st
34         {
35             ins[a[1].ascii] += 1
36         }
37         for a in ch
38         {
39             if ins[a.ascii] == 0
40             {
41                 q.append(a)
42                 res.append(a)
43             }
44         }
45         while(!q.isEmpty)
46         {
47             let c:Character = q.removeFirst()
48             for a in st
49             {
50                 if a[0] == c
51                 {
52                     ins[a[1].ascii] -= 1
53                     if ins[a[1].ascii] == 0
54                     {
55                         q.append(a[1])
56                         res.append(a[1])
57                     }
58                 }
59             }
60         }
61         return res.count == ch.count ? res : String()
62     }
63 }
64 
65 //String擴展
66 extension String {
67     //subscript函數能夠檢索數組中的值
68     //直接按照索引方式截取指定索引的字符
69     subscript (_ i: Int) -> Character {
70         //讀取字符
71         get {return self[index(startIndex, offsetBy: i)]}
72     }
73 }
74 
75 //Character擴展
76 extension Character
77 {
78     //Character轉ASCII整數值(定義小寫爲整數值)
79     var ascii: Int {
80         get {
81             return Int(self.unicodeScalars.first?.value ?? 0)
82         }
83     }
84 }

點擊:Playground測試

1 var sol = Solution()
2 print(sol.alienOrder(["wrt", "wrf", "er", "ett","rftt"]))
3 //Print wertf
4 print(sol.alienOrder(["z","x"]))
5 //Print zx
6 print(sol.alienOrder(["z","x","z"]))
7 //Print ""
相關文章
相關標籤/搜索