Skip to content

Commit 9754316

Browse files
committed
Add solution and test-cases for problem 2273
1 parent f03226b commit 9754316

File tree

3 files changed

+61
-23
lines changed

3 files changed

+61
-23
lines changed

leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [2273.Find Resultant Array After Removing Anagrams][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **0-indexed** string array `words`, where `words[i]` consists of lowercase English letters.
5+
6+
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.
7+
8+
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.
9+
10+
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"`.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: words = ["abba","baba","bbaa","cd","cd"]
16+
Output: ["abba","cd"]
17+
Explanation:
18+
One of the ways we can obtain the resultant array is by using the following operations:
19+
- Since words[2] = "bbaa" and words[1] = "baba" are anagrams, we choose index 2 and delete words[2].
20+
Now words = ["abba","baba","cd","cd"].
21+
- Since words[1] = "baba" and words[0] = "abba" are anagrams, we choose index 1 and delete words[1].
22+
Now words = ["abba","cd","cd"].
23+
- Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2].
24+
Now words = ["abba","cd"].
25+
We can no longer perform any operations, so ["abba","cd"] is the final answer.
1326
```
1427

15-
## 题意
16-
> ...
28+
**Example 2:**
1729

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Find Resultant Array After Removing Anagrams
23-
```go
2430
```
25-
31+
Input: words = ["a","b","c","d","e"]
32+
Output: ["a","b","c","d","e"]
33+
Explanation:
34+
No two adjacent strings in words are anagrams of each other, so no operations are performed.
35+
```
2636

2737
## 结语
2838

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func equal(a, b [26]int) bool {
4+
for i := range 26 {
5+
if a[i] != b[i] {
6+
return false
7+
}
8+
}
9+
return true
10+
}
11+
12+
func Solution(words []string) []string {
13+
l := len(words)
14+
indies := make([][26]int, l)
15+
for index, word := range words {
16+
for _, b := range word {
17+
indies[index][b-'a']++
18+
}
19+
}
20+
index := 0
21+
stack := make([]int, l)
22+
for i := 1; i < l; i++ {
23+
if equal(indies[stack[index]], indies[i]) {
24+
continue
25+
}
26+
index++
27+
stack[index] = i
28+
}
29+
var ret []string
30+
for i := range index + 1 {
31+
ret = append(ret, words[stack[i]])
32+
}
33+
return ret
534
}

leetcode/2201-2300/2273.Find-Resultant-Array-After-Removing-Anagrams/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
expect []string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"abba", "baba", "bbaa", "cd", "cd"}, []string{"abba", "cd"}},
17+
{"TestCase2", []string{"a", "b", "c", "d", "e"}, []string{"a", "b", "c", "d", "e"}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)