Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading