Skip to content

Commit

Permalink
Merge pull request #654 from 0xff-dev/1239
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1239
  • Loading branch information
6boris authored Oct 28, 2023
2 parents 6ce6fd6 + 0b47072 commit 77f65bb
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
# [1239.Maximum Length of a Concatenated String with Unique Characters][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 an array of strings `arr`. A string s is formed by the **concatenation** of a **subsequence** of arr that has **unique characters**.

Return the **maximum** possible length of `s`.

A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All the valid concatenations are:
- ""
- "un"
- "iq"
- "ue"
- "uniq" ("un" + "iq")
- "ique" ("iq" + "ue")
Maximum length is 4.
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Maximum Length of a Concatenated String with Unique Characters
```go
```
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible longest valid concatenations are "chaers" ("cha" + "ers") and "acters" ("act" + "ers").
```

**Example 3:**

```
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Explanation: The only string in arr has all 26 characters.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(arr []string) int {
filter := make([]string, 0)
for _, x := range arr {
tmp := [26]bool{}
add := true
for _, b := range []byte(x) {
if tmp[b-'a'] {
add = false
break
}
tmp[b-'a'] = true
}
if add {
filter = append(filter, x)
}
}
if len(filter) == 0 {
return 0
}

ans := 0
var (
subset func(int, int, int, [26]bool)
canSelect func(a [26]bool, b string) bool
)
canSelect = func(a [26]bool, b string) bool {
for _, v := range b {
if a[v-'a'] {
return false
}
}
return true
}
subset = func(index, subsetLen, strLen int, path [26]bool) {
if subsetLen == 0 {
if strLen > ans {
ans = strLen
}
return
}
if index >= len(filter) {
return
}
if canSelect(path, filter[index]) {
for _, v := range filter[index] {
path[v-'a'] = true
}
subset(index+1, subsetLen-1, strLen+len(filter[index]), path)
for _, v := range filter[index] {
path[v-'a'] = false
}
}
subset(index+1, subsetLen, strLen, path)
}
for l := 1; l <= len(filter); l++ {
subset(0, l, 0, [26]bool{})
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []string{"un", "iq", "ue"}, 4},
{"TestCase2", []string{"cha", "r", "act", "ers"}, 6},
{"TestCase3", []string{"abcdefghijklmnopqrstuvwxyz"}, 26},
{"TestCase4", []string{"abcdefghijklm", "bcdefghijklmn", "cdefghijklmno", "defghijklmnop", "efghijklmnopq", "fghijklmnopqr", "ghijklmnopqrs", "hijklmnopqrst", "ijklmnopqrstu", "jklmnopqrstuv", "klmnopqrstuvw", "lmnopqrstuvwx", "mnopqrstuvwxy", "nopqrstuvwxyz", "opqrstuvwxyza", "pqrstuvwxyzab"}, 26},
}

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

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

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

0 comments on commit 77f65bb

Please sign in to comment.