|
| 1 | +/** |
| 2 | + * [2273] Find Resultant Array After Removing Anagrams |
| 3 | + * |
| 4 | + * You are given a 0-indexed string array words, where words[i] consists of lowercase English letters. |
| 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 | + * 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. |
| 7 | + * 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". |
| 8 | + * |
| 9 | + * Example 1: |
| 10 | + * |
| 11 | + * Input: words = ["abba","baba","bbaa","cd","cd"] |
| 12 | + * Output: ["abba","cd"] |
| 13 | + * Explanation: |
| 14 | + * One of the ways we can obtain the resultant array is by using the following operations: |
| 15 | + * - Since words[2] = "bbaa" and words[1] = "baba" are anagrams, we choose index 2 and delete words[2]. |
| 16 | + * Now words = ["abba","baba","cd","cd"]. |
| 17 | + * - Since words[1] = "baba" and words[0] = "abba" are anagrams, we choose index 1 and delete words[1]. |
| 18 | + * Now words = ["abba","cd","cd"]. |
| 19 | + * - Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2]. |
| 20 | + * Now words = ["abba","cd"]. |
| 21 | + * We can no longer perform any operations, so ["abba","cd"] is the final answer. |
| 22 | + * Example 2: |
| 23 | + * |
| 24 | + * Input: words = ["a","b","c","d","e"] |
| 25 | + * Output: ["a","b","c","d","e"] |
| 26 | + * Explanation: |
| 27 | + * No two adjacent strings in words are anagrams of each other, so no operations are performed. |
| 28 | + * |
| 29 | + * Constraints: |
| 30 | + * |
| 31 | + * 1 <= words.length <= 100 |
| 32 | + * 1 <= words[i].length <= 10 |
| 33 | + * words[i] consists of lowercase English letters. |
| 34 | + * |
| 35 | + */ |
| 36 | +pub struct Solution {} |
| 37 | + |
| 38 | +// problem: https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/ |
| 39 | +// discuss: https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/?currentPage=1&orderBy=most_votes&query= |
| 40 | + |
| 41 | +// submission codes start here |
| 42 | + |
| 43 | +impl Solution { |
| 44 | + pub fn remove_anagrams(words: Vec<String>) -> Vec<String> { |
| 45 | + let maps = words |
| 46 | + .iter() |
| 47 | + .map(|w| { |
| 48 | + let mut lut = [0; 26]; |
| 49 | + for c in w.chars() { |
| 50 | + lut[c as usize - 'a' as usize] += 1; |
| 51 | + } |
| 52 | + lut |
| 53 | + }) |
| 54 | + .collect::<Vec<[i32; 26]>>(); |
| 55 | + |
| 56 | + let mut result = vec![]; |
| 57 | + |
| 58 | + result.push(words[0].clone()); |
| 59 | + |
| 60 | + for i in 1..words.len() { |
| 61 | + if maps[i] != maps[i - 1] { |
| 62 | + result.push(words[i].clone()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + result |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// submission codes end |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + use super::*; |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_2273_example_1() { |
| 78 | + let words = vec_string!["abba", "baba", "bbaa", "cd", "cd"]; |
| 79 | + |
| 80 | + let result = vec_string!["abba", "cd"]; |
| 81 | + |
| 82 | + assert_eq!(Solution::remove_anagrams(words), result); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_2273_example_2() { |
| 87 | + let words = vec_string!["a", "b", "c", "d", "e"]; |
| 88 | + |
| 89 | + let result = vec_string!["a", "b", "c", "d", "e"]; |
| 90 | + |
| 91 | + assert_eq!(Solution::remove_anagrams(words), result); |
| 92 | + } |
| 93 | +} |
0 commit comments