[Swift Weekly Contest 114]LeetCode953. 验证外星语词典 | Verifying an Alien Dictionary

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 of the alphabet, return 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. 1 <= words.length <= 100
  2. 1 <= words[i].length <= 20
  3. order.length == 26
  4. All characters in words[i] and order 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. 1 <= words.length <= 100
  2. 1 <= words[i].length <= 20
  3. order.length == 26
  4. 在 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 }

原文地址:https://www.cnblogs.com/strengthen/p/10090815.html

时间: 2024-07-31 21:28:06

[Swift Weekly Contest 114]LeetCode953. 验证外星语词典 | Verifying an Alien Dictionary的相关文章

[Swift Weekly Contest 114]LeetCode954. 二倍数对数组 | Array of Doubled Pairs

Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2. Example 1: Input: [3,1,3,6] Output: false Example 2: Input: [2,1,2,6] Output:

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you

[Swift Weekly Contest 108]LeetCode932. 漂亮数组 | Beautiful Array

For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such that: For every i < j, there is no k with i < k < j such that A[k] * 2 = A[i] + A[j]. Given N, return any beautiful array A.  (It is guaranteed th

[Swift Weekly Contest 108]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

[Swift Weekly Contest 109]LeetCode933. 最近的请求次数 | Number of Recent Calls

Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t represents some time in milliseconds. Return the number of pings that have been made from 3000 milliseconds ago until now. Any ping with time in [t - 3

[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two islands together to form 1 island. Return the smallest nu

[Swift Weekly Contest 109]LeetCode936. 戳印序列 | Stamping The Sequence

You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the s