返回介绍

solution / 0800-0899 / 0820.Short Encoding of Words / README

发布于 2024-06-17 01:03:34 字数 7709 浏览 0 评论 0 收藏 0

820. 单词的压缩编码

English Version

题目描述

单词数组 words有效编码 由任意助记字符串 s 和下标数组 indices 组成,且满足:

  • words.length == indices.length
  • 助记字符串 s'#' 字符结尾
  • 对于每个下标 indices[i]s 的一个从 indices[i] 开始、到下一个 '#' 字符结束(但不包括 '#')的 子字符串 恰好与 words[i] 相等

给你一个单词数组 words ,返回成功对 words 进行编码的最小助记字符串 s 的长度 。

 

示例 1:

输入:words = ["time", "me", "bell"]
输出:10
解释:一组有效编码为 s = "time#bell#" 和 indices = [0, 2, 5] 。
words[0] = "time" ,s 开始于 indices[0] = 0 到下一个 '#' 结束的子字符串,如加粗部分所示 "time#bell#"
words[1] = "me" ,s 开始于 indices[1] = 2 到下一个 '#' 结束的子字符串,如加粗部分所示 "time#bell#"
words[2] = "bell" ,s 开始于 indices[2] = 5 到下一个 '#' 结束的子字符串,如加粗部分所示 "time#bell#"

示例 2:

输入:words = ["t"]
输出:2
解释:一组有效编码为 s = "t#" 和 indices = [0] 。

 

提示:

  • 1 <= words.length <= 2000
  • 1 <= words[i].length <= 7
  • words[i] 仅由小写字母组成

解法

方法一:前缀树

题目大意:充分利用重叠的后缀,使有效编码尽可能短。

判断当前单词是否是其他单词的后缀,若是,就不用写入助记字符串中,否则需要写入并且加上一个 # 后缀。

class Trie:
  def __init__(self) -> None:
    self.children = [None] * 26


class Solution:
  def minimumLengthEncoding(self, words: List[str]) -> int:
    root = Trie()
    for w in words:
      cur = root
      for c in w[::-1]:
        idx = ord(c) - ord("a")
        if cur.children[idx] == None:
          cur.children[idx] = Trie()
        cur = cur.children[idx]
    return self.dfs(root, 1)

  def dfs(self, cur: Trie, l: int) -> int:
    isLeaf, ans = True, 0
    for i in range(26):
      if cur.children[i] != None:
        isLeaf = False
        ans += self.dfs(cur.children[i], l + 1)
    if isLeaf:
      ans += l
    return ans
class Trie {
  Trie[] children = new Trie[26];
}

class Solution {
  public int minimumLengthEncoding(String[] words) {
    Trie root = new Trie();
    for (String w : words) {
      Trie cur = root;
      for (int i = w.length() - 1; i >= 0; i--) {
        int idx = w.charAt(i) - 'a';
        if (cur.children[idx] == null) {
          cur.children[idx] = new Trie();
        }
        cur = cur.children[idx];
      }
    }
    return dfs(root, 1);
  }

  private int dfs(Trie cur, int l) {
    boolean isLeaf = true;
    int ans = 0;
    for (int i = 0; i < 26; i++) {
      if (cur.children[i] != null) {
        isLeaf = false;
        ans += dfs(cur.children[i], l + 1);
      }
    }
    if (isLeaf) {
      ans += l;
    }
    return ans;
  }
}
struct Trie {
  Trie* children[26] = {nullptr};
};

class Solution {
public:
  int minimumLengthEncoding(vector<string>& words) {
    auto root = new Trie();
    for (auto& w : words) {
      auto cur = root;
      for (int i = w.size() - 1; i >= 0; --i) {
        if (cur->children[w[i] - 'a'] == nullptr) {
          cur->children[w[i] - 'a'] = new Trie();
        }
        cur = cur->children[w[i] - 'a'];
      }
    }
    return dfs(root, 1);
  }

private:
  int dfs(Trie* cur, int l) {
    bool isLeaf = true;
    int ans = 0;
    for (int i = 0; i < 26; ++i) {
      if (cur->children[i] != nullptr) {
        isLeaf = false;
        ans += dfs(cur->children[i], l + 1);
      }
    }
    if (isLeaf) {
      ans += l;
    }
    return ans;
  }
};
type trie struct {
  children [26]*trie
}

func minimumLengthEncoding(words []string) int {
  root := new(trie)
  for _, w := range words {
    cur := root
    for i := len(w) - 1; i >= 0; i-- {
      if cur.children[w[i]-'a'] == nil {
        cur.children[w[i]-'a'] = new(trie)
      }
      cur = cur.children[w[i]-'a']
    }
  }
  return dfs(root, 1)
}

func dfs(cur *trie, l int) int {
  isLeaf, ans := true, 0
  for i := 0; i < 26; i++ {
    if cur.children[i] != nil {
      isLeaf = false
      ans += dfs(cur.children[i], l+1)
    }
  }
  if isLeaf {
    ans += l
  }
  return ans
}

方法二

class Trie:
  def __init__(self):
    self.children = [None] * 26

  def insert(self, w):
    node = self
    pref = True
    for c in w:
      idx = ord(c) - ord("a")
      if node.children[idx] is None:
        node.children[idx] = Trie()
        pref = False
      node = node.children[idx]
    return 0 if pref else len(w) + 1


class Solution:
  def minimumLengthEncoding(self, words: List[str]) -> int:
    words.sort(key=lambda x: -len(x))
    trie = Trie()
    return sum(trie.insert(w[::-1]) for w in words)
class Trie {
  Trie[] children = new Trie[26];

  int insert(String w) {
    Trie node = this;
    boolean pref = true;
    for (int i = w.length() - 1; i >= 0; --i) {
      int idx = w.charAt(i) - 'a';
      if (node.children[idx] == null) {
        pref = false;
        node.children[idx] = new Trie();
      }
      node = node.children[idx];
    }
    return pref ? 0 : w.length() + 1;
  }
}

class Solution {
  public int minimumLengthEncoding(String[] words) {
    Arrays.sort(words, (a, b) -> b.length() - a.length());
    int ans = 0;
    Trie trie = new Trie();
    for (String w : words) {
      ans += trie.insert(w);
    }
    return ans;
  }
}
class Trie {
public:
  vector<Trie*> children;
  Trie()
    : children(26) {}

  int insert(string w) {
    Trie* node = this;
    bool pref = true;
    for (char c : w) {
      c -= 'a';
      if (!node->children[c]) {
        pref = false;
        node->children[c] = new Trie();
      }
      node = node->children[c];
    }
    return pref ? 0 : w.size() + 1;
  }
};

class Solution {
public:
  int minimumLengthEncoding(vector<string>& words) {
    sort(words.begin(), words.end(), [](string& a, string& b) { return a.size() > b.size(); });
    Trie* trie = new Trie();
    int ans = 0;
    for (auto& w : words) {
      reverse(w.begin(), w.end());
      ans += trie->insert(w);
    }
    return ans;
  }
};
type Trie struct {
  children [26]*Trie
}

func newTrie() *Trie {
  return &Trie{}
}

func (this *Trie) insert(w string) int {
  node := this
  pref := true
  for i := len(w) - 1; i >= 0; i-- {
    idx := w[i] - 'a'
    if node.children[idx] == nil {
      pref = false
      node.children[idx] = newTrie()
    }
    node = node.children[idx]
  }
  if pref {
    return 0
  }
  return len(w) + 1
}

func minimumLengthEncoding(words []string) int {
  sort.Slice(words, func(i, j int) bool { return len(words[i]) > len(words[j]) })
  trie := newTrie()
  ans := 0
  for _, w := range words {
    ans += trie.insert(w)
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文