-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy path0819-MostCommonWord.cs
More file actions
43 lines (39 loc) · 1.38 KB
/
0819-MostCommonWord.cs
File metadata and controls
43 lines (39 loc) · 1.38 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//-----------------------------------------------------------------------------
// Runtime: 112ms
// Memory Usage: 25.7 MB
// Link: https://leetcode.com/submissions/detail/352425630/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _0819_MostCommonWord
{
public string MostCommonWord(string paragraph, string[] banned)
{
var split = paragraph.Replace('!', ' ').Replace('?', ' ').Replace('\'', ' ').Replace(',', ' ').Replace(';', ' ').Replace('.', ' ').ToLower().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var bannedSet = new HashSet<string>(banned);
var counts = new Dictionary<string, int>();
foreach (var word in split)
{
if (bannedSet.Contains(word))
continue;
if (counts.ContainsKey(word))
counts[word]++;
else
counts[word] = 1;
}
var max = 0;
var result = string.Empty;
foreach (var pair in counts)
{
if (max < pair.Value)
{
max = pair.Value;
result = pair.Key;
}
}
return result;
}
}
}