Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 4b0410f

Browse files
committed
1048 wrong answer
1 parent daacd55 commit 4b0410f

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

.vscode/settings.json

+15
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"ctaagt",
5757
"ctxt",
5858
"cut",
59+
"czvh",
5960
"deck",
6061
"dfs",
6162
"diff",
@@ -80,6 +81,10 @@
8081
"ghefcdab",
8182
"gitignore",
8283
"goconvey",
84+
"gruj",
85+
"grukj",
86+
"grukkmj",
87+
"grukmj",
8388
"hgfedcba",
8489
"i",
8590
"if",
@@ -94,6 +99,10 @@
9499
"keet",
95100
"keti",
96101
"keto",
102+
"ksqsq",
103+
"ksqvsq",
104+
"ksqvsyq",
105+
"kssq",
97106
"kyxfcuajfassqjfwa",
98107
"laiden",
99108
"leelee",
@@ -156,6 +165,12 @@
156165
"yllw",
157166
"yollew",
158167
"yollow",
168+
"zcpzvh",
169+
"zczpzfvdhx",
170+
"zczpzvdhx",
171+
"zczpzvh",
172+
"zczpzvhx",
173+
"zczvh",
159174
"zppedxfumcfsngp"
160175
],
161176
"cSpell.language": "en,en-US"
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,53 @@
11
package problem1048
22

33
func longestStrChain(words []string) int {
4+
lengths := make([][]string, 18)
5+
minLen := 18
6+
for _, w := range words {
7+
l := len(w)
8+
minLen = min(minLen, l)
9+
lengths[l] = append(lengths[l], w)
10+
}
411

5-
return 0
12+
res := 0
13+
var dfs func(int, string)
14+
dfs = func(count int, w1 string) {
15+
l := len(w1) + 1
16+
if len(lengths[l]) == 0 {
17+
res = max(res, count)
18+
return
19+
}
20+
for _, w2 := range lengths[l] {
21+
if isPredecessor(w1, w2) {
22+
dfs(count+1, w2)
23+
}
24+
}
25+
}
26+
for _, w1 := range lengths[minLen] {
27+
dfs(1, w1)
28+
}
29+
return res
30+
}
31+
32+
func isPredecessor(w1, w2 string) bool {
33+
for i := 0; i < len(w2); i++ {
34+
if w1[:i] == w2[:i] && w1[i:] == w2[i+1:] {
35+
return true
36+
}
37+
}
38+
return false
39+
}
40+
41+
func max(a, b int) int {
42+
if a > b {
43+
return a
44+
}
45+
return b
46+
}
47+
48+
func min(a, b int) int {
49+
if a < b {
50+
return a
51+
}
52+
return b
653
}

Algorithms/1048.longest-string-chain/longest-string-chain_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ var tcs = []struct {
1212
ans int
1313
}{
1414

15+
{
16+
[]string{"ksqvsyq", "ks", "kss", "czvh", "zczpzvdhx", "zczpzvh", "zczpzvhx", "zcpzvh", "zczvh", "gr", "grukmj", "ksqvsq", "gruj", "kssq", "ksqsq", "grukkmj", "grukj", "zczpzfvdhx", "gru"},
17+
7,
18+
},
19+
1520
{
1621
[]string{"a", "b", "ba", "bca", "bda", "bdca"},
1722
4,

0 commit comments

Comments
 (0)