查找出现次数最多的单词 [英] Find word with maximum number of occurrences

查看:103
本文介绍了查找出现次数最多的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索文档中出现次数最多的单词的最佳方法(算法)是什么?

What is the most optimal way (algorithm) to search for the word that has the maximum number of occurrences in a document?

推荐答案

通过简单的直方图,可以在O(n)中查找文档中最常出现的单词 [基于哈希]:

Finding the word that occures most times in a document can be done in O(n) by a simple histogram [hash based]:

histogram <- new map<String,int>
for each word in document: 
   if word in histogram:
      histogram[word] <- histogram[word] + 1
   else:
      histogram[word] <- 1
max <- 0
maxWord<- ""
for each word in histogram:
  if histogram[word] > max:
     max <- histogram[word]
     maxWord <- word
return maxWord

这是O(n)解决方案,并且由于问题显然是Omega(n)问题,因此对于大O符号

This is O(n) solution, and since the problem is clearly Omega(n) problem, it is optimal in terms of big O notation.

这篇关于查找出现次数最多的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆