Skip to content

Commit afe4187

Browse files
committed
Add Most Common Word
1 parent 3ff20d4 commit afe4187

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

LeetCode/819-MostCommonWord.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Problem: https://leetcode.com/problems/most-common-word/
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text.RegularExpressions;
5+
6+
namespace LeetCode {
7+
public partial class Solution {
8+
public string MostCommonWord(string paragraph, string[] banned) {
9+
var wordList = Regex.Replace(paragraph, @"[^\w\s]", " ").ToLower().Split(' ');
10+
var wordCount = new Dictionary<string, int>();
11+
var bannedList = banned.ToList();
12+
13+
for(int i = 0; i < wordList.Length; i++) {
14+
if(wordList[i] == string.Empty || bannedList.Contains(wordList[i])) {
15+
continue;
16+
}
17+
if(wordCount.ContainsKey(wordList[i])) {
18+
var value = wordCount.GetValueOrDefault(wordList[i]);
19+
wordCount[wordList[i]] = value + 1;
20+
}
21+
else {
22+
wordCount.Add(wordList[i], 1);
23+
}
24+
}
25+
26+
return wordCount.OrderByDescending(x => x.Value).FirstOrDefault().Key;
27+
}
28+
}
29+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ LeetCode C# solutions
1010
|**509**| **[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/)** | **[C#](https://github.com/ocimen/leetcode/blob/main/LeetCode/509-FibonacciNumber.cs)** | **Easy** |
1111
|**682**| **[Baseball Game](https://leetcode.com/problems/baseball-game/)** | **[C#](https://github.com/ocimen/leetcode/blob/main/LeetCode/682-BaseballGame.cs)** | **Easy** |
1212
|**771**| **[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)** | **[C#](https://github.com/ocimen/leetcode/blob/main/LeetCode/771-JewelsAndStones.cs)** | **Easy** |
13+
|**819**| **[Most Common Word](https://leetcode.com/problems/most-common-word/)** | **[C#](https://github.com/ocimen/leetcode/blob/main/LeetCode/819-MostCommonWord.cs)** | **Easy** |
1314
|**876**| **[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)** | **[C#](https://github.com/ocimen/leetcode/blob/main/LeetCode/876-MiddleLinkedList.cs)** | **Easy** |
1415
|**933**| **[Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/)** | **[C#](https://github.com/ocimen/leetcode/blob/main/LeetCode/933-NumberofRecentCalls.cs)** | **Easy** |
1516
|**1108**| **[Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/)** | **[C#](https://github.com/ocimen/leetcode/blob/main/LeetCode/1108-DefangingAnIPAddress.cs)** | **Easy** |

0 commit comments

Comments
 (0)