当前位置:网站首页>January 29, 2022: connectives. Give you an array of strings without repeated words
January 29, 2022: connectives. Give you an array of strings without repeated words
2022-06-23 02:45:00 【Fuda scaffold constructor's daily question】
2022-01-29: Conjunctions .
To give you one Without repetition String array of words words , Please find out and return to words All in Conjunctions .
Conjunctions Defined as : A string consisting entirely of at least two shorter words in a given array .
Input :words = "cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"
Output :"catsdogcats","dogcatsdog","ratcatdogcat"
explain :"catsdogcats" from "cats", "dog" and "cats" form ;
"dogcatsdog" from "dog", "cats" and "dog" form ;
"ratcatdogcat" from "rat", "cat", "dog" and "cat" form .
Power button 472.
answer 2022-01-29:
Prefix tree , Try the model from left to right .
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
"sort"
)
func main() {
words := []string{"cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"}
ret := findAllConcatenatedWordsInADict2(words)
fmt.Println(ret)
}
type TrieNode struct {
end bool
nexts []*TrieNode
}
func NewTrieNode() *TrieNode {
ans := &TrieNode{}
ans.end = false
ans.nexts = make([]*TrieNode, 26)
return ans
}
func insert(root *TrieNode, s []byte) {
path0 := 0
for _, c := range s {
path0 = int(c - 'a')
if root.nexts[path0] == nil {
root.nexts[path0] = NewTrieNode()
}
root = root.nexts[path0]
}
root.end = true
}
// Prepare the dynamic planning table in advance
var dp = make([]int, 1000)
// Method 2 : Prefix tree optimization + Dynamic planning optimization
func findAllConcatenatedWordsInADict2(words []string) []string {
ans := make([]string, 0)
if len(words) < 3 {
return ans
}
sort.Slice(words, func(i, j int) bool {
str1 := words[i]
str2 := words[j]
return len(str1) < len(str2)
})
root := NewTrieNode()
for _, str := range words {
s := []byte(str)
for i := 0; i < len(s)+1; i++ {
dp[i] = 0
}
if len(s) > 0 && split2(s, root, 0, dp) {
ans = append(ans, str)
} else {
insert(root, s)
}
}
return ans
}
func split2(s []byte, r *TrieNode, i int, dp []int) bool {
if dp[i] != 0 {
return dp[i] == 1
}
ans := false
if i == len(s) {
ans = true
} else {
c := r
for end := i; end < len(s); end++ {
path0 := s[end] - 'a'
if c.nexts[path0] == nil {
break
}
c = c.nexts[path0]
if c.end && split2(s, r, end+1, dp) {
ans = true
break
}
}
}
dp[i] = twoSelectOne(ans, 1, -1)
return ans
}
func twoSelectOne(c bool, a, b int) int {
if c {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- Evolution history of mobile communication
- Wechat applet camera compressed image is Base64
- January 31, 2022: Maze III. There is a ball in the maze of open spaces and walls. ball
- What is sitelock? What is the function?
- Interviewer: what is the difference between SSH and SSM frameworks? How to choose??
- WM view of commodity master data in SAP retail preliminary level
- What is ISBN code and how to make it
- SetTimeout and setinterval execution time
- How PHP uses redis
- Detailed explanation of various networking modes of video monitoring platform
猜你喜欢

Mongodb aggregate query implements multi table associated query, type conversion, and returns specified parameters.

Spark broadcast variables and accumulators (cases attached)

Evolution history of mobile communication

Nfv and SDN

2021-11-11

5. concept of ruler method

C language series - Section 4 - arrays

Interviewer: what is the difference between SSH and SSM frameworks? How to choose??

Log a log4j2 vulnerability handling

Interviewer: why does TCP shake hands three times and break up four times? Most people can't answer!
随机推荐
5g spectrum
What is sitelock? What is the function?
Analysis of ThreadLocal
8. greed
PHP Base64 image processing Encyclopedia
Log a log4j2 vulnerability handling
Apache Druid's engineering practice in shopee
Docker installs mysql5.7 and mounts the configuration file
Single chip microcomputer (STC series 8051 core single chip microcomputer)
Use of apicloud AVM framework list component list view and flex layout tutorial
Add other view components to the audio and video components of the applet
Nfv and SDN
A penetration of an internal self built shooting range
Essentials of fleet video playback and fleet videoplayer video playback components
5g access network and base station evolution
Detailed explanation of various networking modes of video monitoring platform
Ugui empty button implementation
Goframe framework (RK boot): fast implementation of CSRF verification
How to make word notes beautiful
Deep analysis of time complexity