leetcode/0820.Short-Encoding-of-Words/README.md
A valid encoding of an array of words is any reference string s and array of indices indices such that:
words.length == indices.lengths ends with the '#' character.indices[i], the substring of s starting from indices[i] and up to (but not including) the next '#' character is equal to words[i].Given an array of words, return the length of the shortest reference string s possible of any valid encoding of words.
Example 1:
Input: words = ["time", "me", "bell"]
Output: 10
Explanation: A valid encoding would be s = "time#bell#" and indices = [0, 2, 5].
words[0] = "time", the substring of s starting from indices[0] = 0 to the next '#' is underlined in "time#bell#"
words[1] = "me", the substring of s starting from indices[1] = 2 to the next '#' is underlined in "time#bell#"
words[2] = "bell", the substring of s starting from indices[2] = 5 to the next '#' is underlined in "time#bell#"
Example 2:
Input: words = ["t"]
Output: 2
Explanation: A valid encoding would be s = "t#" and indices = [0].
Constraints:
1 <= words.length <= 20001 <= words[i].length <= 7words[i] consists of only lowercase letters.单词数组 words 的 有效编码 由任意助记字符串 s 和下标数组 indices 组成,且满足:
给你一个单词数组 words ,返回成功对 words 进行编码的最小助记字符串 s 的长度 。
package leetcode
// 解法一 暴力
func minimumLengthEncoding(words []string) int {
res, m := 0, map[string]bool{}
for _, w := range words {
m[w] = true
}
for w := range m {
for i := 1; i < len(w); i++ {
delete(m, w[i:])
}
}
for w := range m {
res += len(w) + 1
}
return res
}
// 解法二 Trie
type node struct {
value byte
sub []*node
}
func (t *node) has(b byte) (*node, bool) {
if t == nil {
return nil, false
}
for i := range t.sub {
if t.sub[i] != nil && t.sub[i].value == b {
return t.sub[i], true
}
}
return nil, false
}
func (t *node) isLeaf() bool {
if t == nil {
return false
}
return len(t.sub) == 0
}
func (t *node) add(s []byte) {
now := t
for i := len(s) - 1; i > -1; i-- {
if v, ok := now.has(s[i]); ok {
now = v
continue
}
temp := new(node)
temp.value = s[i]
now.sub = append(now.sub, temp)
now = temp
}
}
func (t *node) endNodeOf(s []byte) *node {
now := t
for i := len(s) - 1; i > -1; i-- {
if v, ok := now.has(s[i]); ok {
now = v
continue
}
return nil
}
return now
}
func minimumLengthEncoding1(words []string) int {
res, tree, m := 0, new(node), make(map[string]bool)
for i := range words {
if !m[words[i]] {
tree.add([]byte(words[i]))
m[words[i]] = true
}
}
for s := range m {
if tree.endNodeOf([]byte(s)).isLeaf() {
res += len(s)
res++
}
}
return res
}