-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path819-MostCommonWord.cs
29 lines (26 loc) · 1.11 KB
/
819-MostCommonWord.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Problem: https://leetcode.com/problems/most-common-word/
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace LeetCode {
public partial class Solution {
public string MostCommonWord(string paragraph, string[] banned) {
var wordList = Regex.Replace(paragraph, @"[^\w\s]", " ").ToLower().Split(' ');
var wordCount = new Dictionary<string, int>();
var bannedList = banned.ToList();
for(int i = 0; i < wordList.Length; i++) {
if(wordList[i] == string.Empty || bannedList.Contains(wordList[i])) {
continue;
}
if(wordCount.ContainsKey(wordList[i])) {
var value = wordCount.GetValueOrDefault(wordList[i]);
wordCount[wordList[i]] = value + 1;
}
else {
wordCount.Add(wordList[i], 1);
}
}
return wordCount.OrderByDescending(x => x.Value).FirstOrDefault().Key;
}
}
}