Most Common Word, by LeetCode


Problem is this one https://leetcode.com/problems/most-common-word/description/. String and Counting problems are (for the most part) solvable by using the string operators within your programming language (avoid reinventing the wheel, there is no need to do a lot of the parsing by hand anymore. If your programming language doesn’t support the basic string operations, time to switch languages) and hash-tables to quickly access specific substrings. This is exactly the approach that will be taken here. First a quick prep where we add the banned words to a hash-table and we also split all the words in the paragraph. Then it is just a matter of going thru the words in the paragraph, make sure they are not in the banned list, and keep track of the most common used so far. Code is below, Marcelo.

               public class Solution
               {
                              public string MostCommonWord(string paragraph, string[] banned)
                              {
                                             //Prep
                                             Hashtable htBanned = new Hashtable();
                                             foreach (string b in banned) if (!htBanned.ContainsKey(b.ToLower())) htBanned.Add(b.ToLower(), true);
                                             string[] words = paragraph.ToLower().Split(new char[] { ' ', '\n', '\r', '!', '?', '\'', ',', ';', '.' }, StringSplitOptions.RemoveEmptyEntries);

                                             //Process
                                             string mcWord = "";
                                             int mcCount = 0;
                                             Hashtable wordCount = new Hashtable();
                                             foreach (string w in words)
                                                            if (!htBanned.ContainsKey(w))
                                                            {
                                                                           if (!wordCount.ContainsKey(w)) wordCount.Add(w, 0);
                                                                           wordCount[w] = (int)wordCount[w] + 1;
                                                                           if ((int)wordCount[w] > mcCount)
                                                                           {
                                                                                          mcCount = (int)wordCount[w];
                                                                                          mcWord = w;
                                                                           }
                                                            }

                                             return mcWord;
                              }
               }

Comments

  1. As always, sharing a simple solution for a simple problem:

    import collections

    class Solution:
    def mostCommonWord(self, paragraph, banned):
    banned = set(banned)
    return next(word for word, _ in collections.Counter(word.rstrip("!?',;.") for word in paragraph.lower().split()).most_common() if word not in banned)

    Note really a one liner, but fairly close :) A more efficient implementation can use a Trie to store words of the paragraph and banned words. Storing paragraph words in a trie would make increments and checks for banned words O(k) where k == len(word), but oh well :)

    Cheers,
    Taras

    ReplyDelete

Post a Comment

Popular posts from this blog

Advent of Code - Day 6, 2024: BFS and FSM

Golang vs. C#: performance characteristics (simple case study)

Advent of Code - Day 7, 2024: Backtracking and Eval