[Swift Weekly Contest 114]LeetCode953. 驗證外星語詞典 | Verifying an Alien Dictionary
阿新 • • 發佈:2018-12-09
In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order
. The order
of the alphabet is some permutation of lowercase letters.
Given a sequence of words
written in the alien language, and the order
true
if and only if the given words
are sorted lexicographicaly in this alien language.
Example 1:
Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
Example 2:
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
Example 3:
Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).
Note:
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
- All characters in
words[i]
andorder
are english lowercase letters.
某種外星語也使用英文小寫字母,但可能順序 order
不同。字母表的順序(order
)是一些小寫字母的排列。
給定一組用外星語書寫的單詞 words
,以及其字母表的順序 order
,只有當給定的單詞在這種外星語中按字典序排列時,返回 true
;否則,返回 false
。
示例 1:
輸入:words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz" 輸出:true 解釋:在該語言的字母表中,'h' 位於 'l' 之前,所以單詞序列是按字典序排列的。
示例 2:
輸入:words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz" 輸出:false 解釋:在該語言的字母表中,'d' 位於 'l' 之後,那麼 words[0] > words[1],因此單詞序列不是按字典序排列的。
示例 3:
輸入:words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz" 輸出:false 解釋:當前三個字元 "app" 匹配時,第二個字串相對短一些,然後根據詞典編纂規則 "apple" > "app",因為 'l' > '∅',其中 '∅' 是空白字元,定義為比任何其他字元都小(更多資訊)。
提示:
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
- 在
words[i]
和order
中的所有字元都是英文小寫字母。
72ms
1 class Solution { 2 func isAlienSorted(_ words: [String], _ order: String) -> Bool { 3 var words = words 4 var a:[Int] = [Int](repeating:0,count:256) 5 var i:Int = 0 6 var j:Int = 0 7 for i in 0..<order.count 8 { 9 a[order[i].ascii] = i 10 } 11 for i in 0..<words.count 12 { 13 for j in 0..<words[i].count 14 { 15 //a:97 16 words[i][j] = (a[words[i][j].ascii] + 97).ASCII 17 } 18 } 19 for i in 0..<(words.count - 1) 20 { 21 if words[i] > words[i+1] 22 { 23 return false 24 } 25 } 26 return true 27 } 28 } 29 extension String { 30 //subscript函式可以檢索陣列中的值 31 //直接按照索引方式擷取指定索引的字元 32 subscript (_ i: Int) -> Character { 33 //讀取字元 34 get {return self[index(startIndex, offsetBy: i)]} 35 36 //修改字元 37 set 38 { 39 var str:String = self 40 var index = str.index(startIndex, offsetBy: i) 41 str.remove(at: index) 42 str.insert(newValue, at: index) 43 self = str 44 } 45 } 46 } 47 48 //Character擴充套件方法 49 extension Character 50 { 51 //屬性:ASCII整數值(定義小寫為整數值) 52 var ascii: Int { 53 get { 54 let s = String(self).unicodeScalars 55 return Int(s[s.startIndex].value) 56 } 57 } 58 } 59 60 //Int擴充套件方法 61 extension Int 62 { 63 //屬性:ASCII值(定義大寫為字元值) 64 var ASCII:Character 65 { 66 get {return Character(UnicodeScalar(self)!)} 67 } 68 }