|
1 | 1 | // https://leetcode.com/problems/find-resultant-array-after-removing-anagrams
|
2 |
| -// |
| 2 | +// |
3 | 3 | // You are given a **0-indexed** string array `words`, where `words[i]` consists of lowercase English letters.
|
4 |
| -// |
| 4 | +// |
5 | 5 | // In one operation, select any index `i` such that `0 < i < words.length` and `words[i - 1]` and `words[i]` are **anagrams**, and **delete** `words[i]` from `words`. Keep performing this operation as long as you can select an index that satisfies the conditions.
|
6 |
| -// |
| 6 | +// |
7 | 7 | // Return `words` _after performing all operations_. It can be shown that selecting the indices for each operation in **any** arbitrary order will lead to the same result.
|
8 |
| -// |
| 8 | +// |
9 | 9 | // An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase using all the original letters exactly once. For example, `"dacb"` is an anagram of `"abdc"`.
|
10 |
| -// |
| 10 | +// |
11 | 11 | // **Example 1:**
|
12 |
| -// |
| 12 | +// |
13 | 13 | // ```
|
14 | 14 | // **Input:** words = ["abba","baba","bbaa","cd","cd"]
|
15 | 15 | // **Output:** ["abba","cd"]
|
|
22 | 22 | // - Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2].
|
23 | 23 | // Now words = ["abba","cd"].
|
24 | 24 | // We can no longer perform any operations, so ["abba","cd"] is the final answer.```
|
25 |
| -// |
| 25 | +// |
26 | 26 | // **Example 2:**
|
27 |
| -// |
| 27 | +// |
28 | 28 | // ```
|
29 | 29 | // **Input:** words = ["a","b","c","d","e"]
|
30 | 30 | // **Output:** ["a","b","c","d","e"]
|
31 | 31 | // **Explanation:**
|
32 | 32 | // No two adjacent strings in words are anagrams of each other, so no operations are performed.```
|
33 |
| -// |
| 33 | +// |
34 | 34 | // **Constraints:**
|
35 |
| -// |
| 35 | +// |
36 | 36 | // * `1 <= words.length <= 100`
|
37 | 37 | // * `1 <= words[i].length <= 10`
|
38 | 38 | // * `words[i]` consists of lowercase English letters.
|
39 | 39 |
|
40 | 40 | pub fn remove_anagrams(words: Vec<String>) -> Vec<String> {
|
41 |
| - |
| 41 | + let mut result = Vec::new(); |
| 42 | + let mut map = std::collections::HashMap::new(); |
| 43 | + let mut prev_string = String::new(); |
| 44 | + for word in words { |
| 45 | + let mut chars: Vec<char> = word.chars().collect(); |
| 46 | + chars.sort(); |
| 47 | + let key = chars.iter().collect::<String>(); |
| 48 | + if !map.contains_key(&key) { |
| 49 | + map.insert(key.clone(), 1); |
| 50 | + result.push(word); |
| 51 | + } else { |
| 52 | + if prev_string != key { |
| 53 | + result.push(word); |
| 54 | + } |
| 55 | + } |
| 56 | + prev_string = key.clone(); |
42 | 57 | }
|
| 58 | + return result; |
| 59 | +} |
43 | 60 |
|
44 | 61 | #[test]
|
45 | 62 | pub fn t1() {
|
| 63 | + assert_eq!( |
| 64 | + remove_anagrams(vec![ |
| 65 | + "abba".to_string(), |
| 66 | + "baba".to_string(), |
| 67 | + "bbaa".to_string(), |
| 68 | + "cd".to_string(), |
| 69 | + "cd".to_string() |
| 70 | + ]), |
| 71 | + vec!["abba".to_string(), "cd".to_string()] |
| 72 | + ); |
| 73 | + assert_eq!( |
| 74 | + remove_anagrams(vec![ |
| 75 | + "a".to_string(), |
| 76 | + "b".to_string(), |
| 77 | + "c".to_string(), |
| 78 | + "d".to_string(), |
| 79 | + "e".to_string() |
| 80 | + ]), |
| 81 | + vec![ |
| 82 | + "a".to_string(), |
| 83 | + "b".to_string(), |
| 84 | + "c".to_string(), |
| 85 | + "d".to_string(), |
| 86 | + "e".to_string() |
| 87 | + ] |
| 88 | + ); |
46 | 89 | }
|
0 commit comments