Skip to content

Commit f57ae66

Browse files
committed
feat: add more
1 parent 8ee90fe commit f57ae66

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// https://leetcode.com/problems/find-resultant-array-after-removing-anagrams
2-
//
2+
//
33
// You are given a **0-indexed** string array `words`, where `words[i]` consists of lowercase English letters.
4-
//
4+
//
55
// 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+
//
77
// 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+
//
99
// 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+
//
1111
// **Example 1:**
12-
//
12+
//
1313
// ```
1414
// **Input:** words = ["abba","baba","bbaa","cd","cd"]
1515
// **Output:** ["abba","cd"]
@@ -22,25 +22,68 @@
2222
// - Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2].
2323
// Now words = ["abba","cd"].
2424
// We can no longer perform any operations, so ["abba","cd"] is the final answer.```
25-
//
25+
//
2626
// **Example 2:**
27-
//
27+
//
2828
// ```
2929
// **Input:** words = ["a","b","c","d","e"]
3030
// **Output:** ["a","b","c","d","e"]
3131
// **Explanation:**
3232
// No two adjacent strings in words are anagrams of each other, so no operations are performed.```
33-
//
33+
//
3434
// **Constraints:**
35-
//
35+
//
3636
// * `1 <= words.length <= 100`
3737
// * `1 <= words[i].length <= 10`
3838
// * `words[i]` consists of lowercase English letters.
3939

4040
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();
4257
}
58+
return result;
59+
}
4360

4461
#[test]
4562
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+
);
4689
}

Array/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ mod _2229_check_if_an_array_is_consecutive;
155155
mod _2235_add_two_integers;
156156
mod _2248_intersection_of_multiple_arrays;
157157
mod _2255_count_prefixes_of_a_given_string;
158+
mod _2273_find_resultant_array_after_removing_anagrams;
158159
mod _2293_min_max_game;
159160
mod _2319_check_if_matrix_is_x_matrix;
160161
mod _2335_minimum_amount_of_time_to_fill_cups;

0 commit comments

Comments
 (0)