Skip to content

Commit 66891f2

Browse files
committed
feat: add more
1 parent a84205c commit 66891f2

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed
Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,83 @@
11
// https://leetcode.com/problems/verifying-an-alien-dictionary
2-
//
2+
//
33
// 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+
//
55
// 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+
//
77
// **Example 1:**
8-
//
8+
//
99
// ```
1010
// **Input:** words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
1111
// **Output:** true
1212
// **Explanation:** As 'h' comes before 'l' in this language, then the sequence is sorted.
1313
// ```
14-
//
14+
//
1515
// **Example 2:**
16-
//
16+
//
1717
// ```
1818
// **Input:** words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
1919
// **Output:** false
2020
// **Explanation:** As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
2121
// ```
22-
//
22+
//
2323
// **Example 3:**
24-
//
24+
//
2525
// ```
2626
// **Input:** words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
2727
// **Output:** false
2828
// **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)).
2929
// ```
30-
//
30+
//
3131
// **Constraints:**
32-
//
32+
//
3333
// * `1 <= words.length <= 100`
3434
// * `1 <= words[i].length <= 20`
3535
// * `order.length == 26`
3636
// * All characters in `words[i]` and `order` are English lowercase letters.
3737

3838
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+
}
4063
}
64+
return true;
65+
}
4166

4267
#[test]
4368
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+
);
4483
}

Array/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ mod _0908_smallest_range_i;
4242
mod _0922_sort_array_by_parity_ii;
4343
mod _0929_unique_email_addresses;
4444
mod _0944_delete_columns_to_make_sorted;
45+
mod _0953_verifying_an_alien_dictionary;
4546
mod _0961_n_repeated_element_in_size_2n_array;
4647
mod _0977_squares_of_a_sorted_array;
4748
mod _0999_available_captures_for_rook;

0 commit comments

Comments
 (0)