-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #654 from 0xff-dev/1239
Add solution and test-cases for problem 1239
- Loading branch information
Showing
3 changed files
with
93 additions
and
22 deletions.
There are no files selected for viewing
40 changes: 27 additions & 13 deletions
40
...0/1239.Maximum-Length-of-a-Concatenated-String-with-Unique-Characters/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 58 additions & 2 deletions
60
...1201-1300/1239.Maximum-Length-of-a-Concatenated-String-with-Unique-Characters/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters