0819. Most Common Word

819. Most Common Word #

题目 #

Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn’t banned, and that the answer is unique.

Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase.

Example:

Input: 
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball"
Explanation: 
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. 
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"), 
and that "hit" isn't the answer even though it occurs more because it is banned.

Note:

  • 1 <= paragraph.length <= 1000.
  • 0 <= banned.length <= 100.
  • 1 <= banned[i].length <= 10.
  • The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.)
  • paragraph only consists of letters, spaces, or the punctuation symbols !?',;.
  • There are no hyphens or hyphenated words.
  • Words only consist of letters, never apostrophes or other punctuation symbols.

题目大意 #

给定一个段落 (paragraph) 和一个禁用单词列表 (banned)。返回出现次数最多,同时不在禁用列表中的单词。题目保证至少有一个词不在禁用列表中,而且答案唯一。

禁用列表中的单词用小写字母表示,不含标点符号。段落中的单词不区分大小写。答案都是小写字母。

解题思路 #

  • 给出一段话和一个 banned 的字符串,要求输出这段话中出现频次最高的并且不出现在 banned 数组里面的字符串,答案唯一。这题是简单题,依次统计每个单词的频次,然后从 map 中删除 banned 里面的单词,取出剩下频次最高的单词即可。

代码 #


package leetcode

import "strings"

func mostCommonWord(paragraph string, banned []string) string {
	freqMap, start := make(map[string]int), -1
	for i, c := range paragraph {
		if c == ' ' || c == '!' || c == '?' || c == '\'' || c == ',' || c == ';' || c == '.' {
			if start > -1 {
				word := strings.ToLower(paragraph[start:i])
				freqMap[word]++
			}
			start = -1
		} else {
			if start == -1 {
				start = i
			}
		}
	}
	if start != -1 {
		word := strings.ToLower(paragraph[start:])
		freqMap[word]++
	}
	// Strip the banned words from the freqmap
	for _, bannedWord := range banned {
		delete(freqMap, bannedWord)
	}
	// Find most freq word
	mostFreqWord, mostFreqCount := "", 0
	for word, freq := range freqMap {
		if freq > mostFreqCount {
			mostFreqWord = word
			mostFreqCount = freq
		}
	}
	return mostFreqWord
}


⬅️上一页

下一页➡️

Calendar Apr 8, 2023
Edit Edit this page
本站总访问量:  次 您是本站第  位访问者