返回介绍

lcci / 17.13.Re-Space / README_EN

发布于 2024-06-17 01:04:42 字数 3796 浏览 0 评论 0 收藏 0

17.13. Re-Space

中文文档

Description

Oh, no! You have accidentally removed all spaces, punctuation, and capitalization in a lengthy document. A sentence like "I reset the computer. It still didn't boot!" became "iresetthecomputeritstilldidntboot''. You'll deal with the punctuation and capi­talization later; right now you need to re-insert the spaces. Most of the words are in a dictionary but a few are not. Given a dictionary (a list of strings) and the document (a string), design an algorithm to unconcatenate the document in a way that minimizes the number of unrecognized characters. Return the number of unrecognized characters.

Note: This problem is slightly different from the original one in the book.

 

Example:


Input: 

dictionary = ["looked","just","like","her","brother"]

sentence = "jesslookedjustliketimherbrother"

Output:  7

Explanation:  After unconcatenating, we got "jess looked just like tim her brother", which containing 7 unrecognized characters.

Note:

  • 0 <= len(sentence) <= 1000
  • The total number of characters in dictionary is less than or equal to 150000.
  • There are only lowercase letters in dictionary and sentence.

Solutions

Solution 1

class Solution:
  def respace(self, dictionary: List[str], sentence: str) -> int:
    s = set(dictionary)
    n = len(sentence)
    dp = [0] * (n + 1)
    for i in range(1, n + 1):
      dp[i] = dp[i - 1] + 1
      for j in range(i):
        if sentence[j:i] in s:
          dp[i] = min(dp[i], dp[j])
    return dp[-1]
class Solution {
  public int respace(String[] dictionary, String sentence) {
    Set<String> dict = new HashSet<>(Arrays.asList(dictionary));
    int n = sentence.length();
    int[] dp = new int[n + 1];
    for (int i = 1; i <= n; i++) {
      dp[i] = dp[i - 1] + 1;
      for (int j = 0; j < i; ++j) {
        if (dict.contains(sentence.substring(j, i))) {
          dp[i] = Math.min(dp[i], dp[j]);
        }
      }
    }
    return dp[n];
  }
}
class Solution {
public:
  int respace(vector<string>& dictionary, string sentence) {
    unordered_set<string> s(dictionary.begin(), dictionary.end());
    int n = sentence.size();
    vector<int> dp(n + 1);
    for (int i = 1; i <= n; ++i) {
      dp[i] = dp[i - 1] + 1;
      for (int j = 0; j < i; ++j) {
        if (s.count(sentence.substr(j, i - j))) {
          dp[i] = min(dp[i], dp[j]);
        }
      }
    }
    return dp[n];
  }
};
func respace(dictionary []string, sentence string) int {
  s := map[string]bool{}
  for _, v := range dictionary {
    s[v] = true
  }
  n := len(sentence)
  dp := make([]int, n+1)
  for i := 1; i <= n; i++ {
    dp[i] = dp[i-1] + 1
    for j := 0; j < i; j++ {
      if s[sentence[j:i]] {
        dp[i] = min(dp[i], dp[j])
      }
    }
  }
  return dp[n]
}

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

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

发布评论

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