From 975431672fc38589d894d819021c8d8a4b15abc2 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 13 Oct 2025 09:02:55 +0800 Subject: [PATCH] Add solution and test-cases for problem 2273 --- .../README.md | 38 ++++++++++++------- .../Solution.go | 33 +++++++++++++++- .../Solution_test.go | 13 +++---- 3 files changed, 61 insertions(+), 23 deletions(-) diff --git a/leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/README.md b/leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/README.md index 631acb1f4..4fa961fd0 100755 --- a/leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/README.md +++ b/leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/README.md @@ -1,28 +1,38 @@ # [2273.Find Resultant Array After Removing Anagrams][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given a **0-indexed** string array `words`, where `words[i]` consists of lowercase English letters. + +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. + +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. + +An **Anagrams** 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"`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: words = ["abba","baba","bbaa","cd","cd"] +Output: ["abba","cd"] +Explanation: +One of the ways we can obtain the resultant array is by using the following operations: +- Since words[2] = "bbaa" and words[1] = "baba" are anagrams, we choose index 2 and delete words[2]. + Now words = ["abba","baba","cd","cd"]. +- Since words[1] = "baba" and words[0] = "abba" are anagrams, we choose index 1 and delete words[1]. + Now words = ["abba","cd","cd"]. +- Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2]. + Now words = ["abba","cd"]. +We can no longer perform any operations, so ["abba","cd"] is the final answer. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Find Resultant Array After Removing Anagrams -```go ``` - +Input: words = ["a","b","c","d","e"] +Output: ["a","b","c","d","e"] +Explanation: +No two adjacent strings in words are anagrams of each other, so no operations are performed. +``` ## 结语 diff --git a/leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/Solution.go b/leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/Solution.go index d115ccf5e..730080a7a 100644 --- a/leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/Solution.go +++ b/leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/Solution.go @@ -1,5 +1,34 @@ package Solution -func Solution(x bool) bool { - return x +func equal(a, b [26]int) bool { + for i := range 26 { + if a[i] != b[i] { + return false + } + } + return true +} + +func Solution(words []string) []string { + l := len(words) + indies := make([][26]int, l) + for index, word := range words { + for _, b := range word { + indies[index][b-'a']++ + } + } + index := 0 + stack := make([]int, l) + for i := 1; i < l; i++ { + if equal(indies[stack[index]], indies[i]) { + continue + } + index++ + stack[index] = i + } + var ret []string + for i := range index + 1 { + ret = append(ret, words[stack[i]]) + } + return ret } diff --git a/leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/Solution_test.go b/leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/Solution_test.go index 14ff50eb4..30898ef83 100644 --- a/leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/Solution_test.go +++ b/leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []string + expect []string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []string{"abba", "baba", "bbaa", "cd", "cd"}, []string{"abba", "cd"}}, + {"TestCase2", []string{"a", "b", "c", "d", "e"}, []string{"a", "b", "c", "d", "e"}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }