From 975c32502b8222fba7395314865a5869cdc5fa88 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 13 Oct 2025 07:01:56 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2273 --- .../README.md | 67 +++++++++++++++++++ .../README_EN.md | 67 +++++++++++++++++++ .../Solution.js | 28 ++++++++ .../Solution.rs | 29 ++++++++ 4 files changed, 191 insertions(+) create mode 100644 solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/Solution.js create mode 100644 solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/Solution.rs diff --git a/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/README.md b/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/README.md index 40f445ab7b439..c3288257bae11 100644 --- a/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/README.md +++ b/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/README.md @@ -230,6 +230,73 @@ function removeAnagrams(words: string[]): string[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn remove_anagrams(words: Vec) -> Vec { + fn check(s: &str, t: &str) -> bool { + if s.len() != t.len() { + return true; + } + let mut cnt = [0; 26]; + for c in s.bytes() { + cnt[(c - b'a') as usize] += 1; + } + for c in t.bytes() { + let idx = (c - b'a') as usize; + cnt[idx] -= 1; + if cnt[idx] < 0 { + return true; + } + } + false + } + + let mut ans = vec![words[0].clone()]; + for i in 1..words.len() { + if check(&words[i - 1], &words[i]) { + ans.push(words[i].clone()); + } + } + ans + } +} +``` + +#### JavaScript + +```js +/** + * @param {string[]} words + * @return {string[]} + */ +var removeAnagrams = function (words) { + const ans = [words[0]]; + const check = (s, t) => { + if (s.length !== t.length) { + return true; + } + const cnt = Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt() - 97]; + } + for (const c of t) { + if (--cnt[c.charCodeAt() - 97] < 0) { + return true; + } + } + return false; + }; + for (let i = 1; i < words.length; ++i) { + if (check(words[i - 1], words[i])) { + ans.push(words[i]); + } + } + return ans; +}; +``` + diff --git a/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/README_EN.md b/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/README_EN.md index e65b6c910d6b5..92e87e36a2a31 100644 --- a/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/README_EN.md +++ b/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/README_EN.md @@ -223,6 +223,73 @@ function removeAnagrams(words: string[]): string[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn remove_anagrams(words: Vec) -> Vec { + fn check(s: &str, t: &str) -> bool { + if s.len() != t.len() { + return true; + } + let mut cnt = [0; 26]; + for c in s.bytes() { + cnt[(c - b'a') as usize] += 1; + } + for c in t.bytes() { + let idx = (c - b'a') as usize; + cnt[idx] -= 1; + if cnt[idx] < 0 { + return true; + } + } + false + } + + let mut ans = vec![words[0].clone()]; + for i in 1..words.len() { + if check(&words[i - 1], &words[i]) { + ans.push(words[i].clone()); + } + } + ans + } +} +``` + +#### JavaScript + +```js +/** + * @param {string[]} words + * @return {string[]} + */ +var removeAnagrams = function (words) { + const ans = [words[0]]; + const check = (s, t) => { + if (s.length !== t.length) { + return true; + } + const cnt = Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt() - 97]; + } + for (const c of t) { + if (--cnt[c.charCodeAt() - 97] < 0) { + return true; + } + } + return false; + }; + for (let i = 1; i < words.length; ++i) { + if (check(words[i - 1], words[i])) { + ans.push(words[i]); + } + } + return ans; +}; +``` + diff --git a/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/Solution.js b/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/Solution.js new file mode 100644 index 0000000000000..0c63169594c43 --- /dev/null +++ b/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/Solution.js @@ -0,0 +1,28 @@ +/** + * @param {string[]} words + * @return {string[]} + */ +var removeAnagrams = function (words) { + const ans = [words[0]]; + const check = (s, t) => { + if (s.length !== t.length) { + return true; + } + const cnt = Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt() - 97]; + } + for (const c of t) { + if (--cnt[c.charCodeAt() - 97] < 0) { + return true; + } + } + return false; + }; + for (let i = 1; i < words.length; ++i) { + if (check(words[i - 1], words[i])) { + ans.push(words[i]); + } + } + return ans; +}; diff --git a/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/Solution.rs b/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/Solution.rs new file mode 100644 index 0000000000000..4db0554f48afc --- /dev/null +++ b/solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/Solution.rs @@ -0,0 +1,29 @@ +impl Solution { + pub fn remove_anagrams(words: Vec) -> Vec { + fn check(s: &str, t: &str) -> bool { + if s.len() != t.len() { + return true; + } + let mut cnt = [0; 26]; + for c in s.bytes() { + cnt[(c - b'a') as usize] += 1; + } + for c in t.bytes() { + let idx = (c - b'a') as usize; + cnt[idx] -= 1; + if cnt[idx] < 0 { + return true; + } + } + false + } + + let mut ans = vec![words[0].clone()]; + for i in 1..words.len() { + if check(&words[i - 1], &words[i]) { + ans.push(words[i].clone()); + } + } + ans + } +}