- 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/
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text.RegularExpressions;
namespaceLeetCode{
publicpartialclassSolution{
publicstringMostCommonWord(stringparagraph,string[]banned){
varwordList=Regex.Replace(paragraph,@"[^\w\s]"," ").ToLower().Split(' ');
varwordCount=newDictionary<string,int>();
varbannedList=banned.ToList();
for(inti=0;i<wordList.Length;i++){
if(wordList[i]==string.Empty||bannedList.Contains(wordList[i])){
continue;
}
if(wordCount.ContainsKey(wordList[i])){
varvalue=wordCount.GetValueOrDefault(wordList[i]);
wordCount[wordList[i]]=value+1;
}
else{
wordCount.Add(wordList[i],1);
}
}
returnwordCount.OrderByDescending(x =>x.Value).FirstOrDefault().Key;
}
}
}