|
1 | 1 | // https://leetcode.com/problems/verifying-an-alien-dictionary
|
2 |
| -// |
| 2 | +// |
3 | 3 | // 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.
|
4 |
| -// |
| 4 | +// |
5 | 5 | // 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 lexicographically in this alien language.
|
6 |
| -// |
| 6 | +// |
7 | 7 | // **Example 1:**
|
8 |
| -// |
| 8 | +// |
9 | 9 | // ```
|
10 | 10 | // **Input:** words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
|
11 | 11 | // **Output:** true
|
12 | 12 | // **Explanation:** As 'h' comes before 'l' in this language, then the sequence is sorted.
|
13 | 13 | // ```
|
14 |
| -// |
| 14 | +// |
15 | 15 | // **Example 2:**
|
16 |
| -// |
| 16 | +// |
17 | 17 | // ```
|
18 | 18 | // **Input:** words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
|
19 | 19 | // **Output:** false
|
20 | 20 | // **Explanation:** As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
|
21 | 21 | // ```
|
22 |
| -// |
| 22 | +// |
23 | 23 | // **Example 3:**
|
24 |
| -// |
| 24 | +// |
25 | 25 | // ```
|
26 | 26 | // **Input:** words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
|
27 | 27 | // **Output:** false
|
28 | 28 | // **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](https://en.wikipedia.org/wiki/Lexicographical_order)).
|
29 | 29 | // ```
|
30 |
| -// |
| 30 | +// |
31 | 31 | // **Constraints:**
|
32 |
| -// |
| 32 | +// |
33 | 33 | // * `1 <= words.length <= 100`
|
34 | 34 | // * `1 <= words[i].length <= 20`
|
35 | 35 | // * `order.length == 26`
|
36 | 36 | // * All characters in `words[i]` and `order` are English lowercase letters.
|
37 | 37 |
|
38 | 38 | pub fn is_alien_sorted(words: Vec<String>, order: String) -> bool {
|
39 |
| - |
| 39 | + if words.len() == 1 { |
| 40 | + return true; |
| 41 | + } |
| 42 | + let order_map = order |
| 43 | + .chars() |
| 44 | + .enumerate() |
| 45 | + .map(|(i, c)| (c, i)) |
| 46 | + .collect::<std::collections::HashMap<char, usize>>(); |
| 47 | + for i in 0..words.len() - 1 { |
| 48 | + let word1 = words[i].chars().collect::<Vec<char>>(); |
| 49 | + let word2 = words[i + 1].chars().collect::<Vec<char>>(); |
| 50 | + let mut j = 0; |
| 51 | + while j < word1.len() && j < word2.len() { |
| 52 | + if order_map[&word1[j]] < order_map[&word2[j]] { |
| 53 | + break; |
| 54 | + } else if order_map[&word1[j]] == order_map[&word2[j]] { |
| 55 | + j += 1; |
| 56 | + } else { |
| 57 | + return false; |
| 58 | + } |
| 59 | + } |
| 60 | + if j == word2.len() && j < word1.len() { |
| 61 | + return false; |
| 62 | + } |
40 | 63 | }
|
| 64 | + return true; |
| 65 | +} |
41 | 66 |
|
42 | 67 | #[test]
|
43 | 68 | pub fn t1() {
|
| 69 | + assert_eq!( |
| 70 | + is_alien_sorted( |
| 71 | + vec!["hello".to_string(), "leetcode".to_string()], |
| 72 | + "hlabcdefgijkmnopqrstuvwxyz".to_string() |
| 73 | + ), |
| 74 | + true |
| 75 | + ); |
| 76 | + assert_eq!( |
| 77 | + is_alien_sorted( |
| 78 | + vec!["apple".to_string(), "app".to_string()], |
| 79 | + "abcdefghijklmnopqrstuvwxyz".to_string() |
| 80 | + ), |
| 81 | + false |
| 82 | + ); |
44 | 83 | }
|
0 commit comments