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

Commit 9e9f8fb

Browse files
committed
1048 accepted. 16ms
1 parent 4b0410f commit 9e9f8fb

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@ func longestStrChain(words []string) int {
1212
res := 0
1313
var dfs func(int, string)
1414
dfs = func(count int, w1 string) {
15+
res = max(res, count)
1516
l := len(w1) + 1
16-
if len(lengths[l]) == 0 {
17-
res = max(res, count)
18-
return
19-
}
20-
for _, w2 := range lengths[l] {
17+
for i, w2 := range lengths[l] {
2118
if isPredecessor(w1, w2) {
2219
dfs(count+1, w2)
20+
lengths[l][i] = ""
2321
}
2422
}
2523
}
26-
for _, w1 := range lengths[minLen] {
27-
dfs(1, w1)
24+
for i := 0; i < 17; i++ {
25+
for _, w1 := range lengths[i] {
26+
if w1 != "" {
27+
dfs(1, w1)
28+
}
29+
}
2830
}
2931
return res
3032
}

0 commit comments

Comments
 (0)