返回介绍

lcci / 16.02.Words Frequency / README

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

面试题 16.02. 单词频率

English Version

题目描述

设计一个方法,找出任意指定单词在一本书中的出现频率。

你的实现应该支持如下操作:

  • WordsFrequency(book)构造函数,参数为字符串数组构成的一本书
  • get(word)查询指定单词在数中出现的频率

示例:

WordsFrequency wordsFrequency = new WordsFrequency({"i", "have", "an", "apple", "he", "have", "a", "pen"});
wordsFrequency.get("you"); //返回0,"you"没有出现过
wordsFrequency.get("have"); //返回2,"have"出现2次
wordsFrequency.get("an"); //返回1
wordsFrequency.get("apple"); //返回1
wordsFrequency.get("pen"); //返回1

提示:

  • book[i]中只包含小写字母
  • 1 <= book.length <= 100000
  • 1 <= book[i].length <= 10
  • get函数的调用次数不会超过100000

解法

方法一:哈希表

我们用哈希表 $cnt$ 统计 $book$ 中每个单词出现的次数。

调用 get 函数时,我们只需要返回 $cnt$ 中对应的单词的出现次数即可。

时间复杂度方面,初始化哈希表 $cnt$ 的时间复杂度为 $O(n)$,其中 $n$ 为 $book$ 的长度。get 函数的时间复杂度为 $O(1)$。空间复杂度为 $O(n)$。

class WordsFrequency:
  def __init__(self, book: List[str]):
    self.cnt = Counter(book)

  def get(self, word: str) -> int:
    return self.cnt[word]


# Your WordsFrequency object will be instantiated and called as such:
# obj = WordsFrequency(book)
# param_1 = obj.get(word)
class WordsFrequency {
  private Map<String, Integer> cnt = new HashMap<>();

  public WordsFrequency(String[] book) {
    for (String x : book) {
      cnt.merge(x, 1, Integer::sum);
    }
  }

  public int get(String word) {
    return cnt.getOrDefault(word, 0);
  }
}

/**
 * Your WordsFrequency object will be instantiated and called as such:
 * WordsFrequency obj = new WordsFrequency(book);
 * int param_1 = obj.get(word);
 */
class WordsFrequency {
public:
  WordsFrequency(vector<string>& book) {
    for (auto& x : book) {
      ++cnt[x];
    }
  }

  int get(string word) {
    return cnt[word];
  }

private:
  unordered_map<string, int> cnt;
};

/**
 * Your WordsFrequency object will be instantiated and called as such:
 * WordsFrequency* obj = new WordsFrequency(book);
 * int param_1 = obj->get(word);
 */
type WordsFrequency struct {
  cnt map[string]int
}

func Constructor(book []string) WordsFrequency {
  cnt := map[string]int{}
  for _, x := range book {
    cnt[x]++
  }
  return WordsFrequency{cnt}
}

func (this *WordsFrequency) Get(word string) int {
  return this.cnt[word]
}

/**
 * Your WordsFrequency object will be instantiated and called as such:
 * obj := Constructor(book);
 * param_1 := obj.Get(word);
 */
class WordsFrequency {
  private cnt: Map<string, number>;

  constructor(book: string[]) {
    const cnt = new Map<string, number>();
    for (const word of book) {
      cnt.set(word, (cnt.get(word) ?? 0) + 1);
    }
    this.cnt = cnt;
  }

  get(word: string): number {
    return this.cnt.get(word) ?? 0;
  }
}

/**
 * Your WordsFrequency object will be instantiated and called as such:
 * var obj = new WordsFrequency(book)
 * var param_1 = obj.get(word)
 */
use std::collections::HashMap;
struct WordsFrequency {
  cnt: HashMap<String, i32>,
}

/**
 * `&self` means the method takes an immutable reference.
 * If you need a mutable reference, change it to `&mut self` instead.
 */
impl WordsFrequency {
  fn new(book: Vec<String>) -> Self {
    let mut cnt = HashMap::new();
    for word in book.into_iter() {
      *cnt.entry(word).or_insert(0) += 1;
    }
    Self { cnt }
  }

  fn get(&self, word: String) -> i32 {
    *self.cnt.get(&word).unwrap_or(&0)
  }
}/**
 * Your WordsFrequency object will be instantiated and called as such:
 * let obj = WordsFrequency::new(book);
 * let ret_1: i32 = obj.get(word);
 */
/**
 * @param {string[]} book
 */
var WordsFrequency = function (book) {
  this.cnt = new Map();
  for (const x of book) {
    this.cnt.set(x, (this.cnt.get(x) || 0) + 1);
  }
};

/**
 * @param {string} word
 * @return {number}
 */
WordsFrequency.prototype.get = function (word) {
  return this.cnt.get(word) || 0;
};

/**
 * Your WordsFrequency object will be instantiated and called as such:
 * var obj = new WordsFrequency(book)
 * var param_1 = obj.get(word)
 */

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

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

发布评论

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