Most Common Word
LeetCode 837 | Difficulty: Easyβ
EasyProblem Descriptionβ
Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.
The words in paragraph are case-insensitive and the answer should be returned in lowercase.
Note that words can not contain punctuation symbols.
Example 1:
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.
Example 2:
Input: paragraph = "a.", banned = []
Output: "a"
Constraints:
- `1 <= paragraph.length <= 1000`
- paragraph consists of English letters, space `' '`, or one of the symbols: `"!?',;."`.
- `0 <= banned.length <= 100`
- `1 <= banned[i].length <= 10`
- `banned[i]` consists of only lowercase English letters.
Topics: Array, Hash Table, String, Counting
Approachβ
Hash Mapβ
Use a hash map for O(1) average lookups. Store seen values, frequencies, or indices. The key question: what should I store as key, and what as value?
Need fast lookups, counting frequencies, finding complements/pairs.
String Processingβ
Consider character frequency counts, two-pointer approaches, or building strings efficiently. For pattern matching, think about KMP or rolling hash. For palindromes, expand from center or use DP.
Anagram detection, palindrome checking, string transformation, pattern matching.
Solutionsβ
Solution 1: C# (Best: 160 ms)β
| Metric | Value |
|---|---|
| Runtime | 160 ms |
| Memory | N/A |
| Date | 2018-06-26 |
using System.Text.RegularExpressions;
public class Solution {
public string MostCommonWord(string paragraph, string[] banned) {
Dictionary<string,int> wordCount = new Dictionary<string, int>();
string pattern = @"[!?',;.]";
paragraph = Regex.Replace(paragraph,pattern," ");
var allWords = paragraph.Split(new char[] { ' '}, StringSplitOptions.RemoveEmptyEntries).Select(x=>x.ToLower());
foreach (var word in allWords)
{
if (!banned.Contains(word))
{
if (wordCount.ContainsKey(word))
{
wordCount[word]++;
}
else{
wordCount.Add(word,1);
}
}
}
int max=Int32.MinValue;
string maxRepeatedWord = "";
foreach (var wCnt in wordCount)
{
if(max<wCnt.Value)
{
max=wCnt.Value;
maxRepeatedWord = wCnt.Key;
}
}
return maxRepeatedWord;
}
}
π 1 more C# submission(s)
Submission (2018-06-26) β 168 ms, N/Aβ
using System.Text.RegularExpressions;
public class Solution {
public string MostCommonWord(string paragraph, string[] banned) {
Dictionary<string,int> wordsCount = new Dictionary<string, int>();
string pattern = @"[!?',;.]";
paragraph = Regex.Replace(paragraph, pattern, " ");
var allWords = paragraph.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries).Select(x=>x.ToLower());
int max = Int32.MinValue;
string maxRepeatedWord = "";
foreach (var word in allWords)
{
if (!banned.Contains(word))
{
if (wordsCount.ContainsKey(word))
{
wordsCount[word]++;
}
else{
wordsCount.Add(word,1);
}
if (max < wordsCount[word])
{
max = wordsCount[word];
maxRepeatedWord = word;
}
}
}
return maxRepeatedWord;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Hash Map | $O(n)$ | $O(n)$ |
Interview Tipsβ
- Start by clarifying edge cases: empty input, single element, all duplicates.
- Hash map gives O(1) lookup β think about what to use as key vs value.