[case of algorithm thousand questions] daily LeetCode punch in - 97. The most common words

Posted by php_east on Sun, 26 Dec 2021 23:02:14 +0100

๐Ÿ“ข preface

๐Ÿš€ Algorithm problem ๐Ÿš€
  • ๐ŸŒฒ Punching out an algorithm problem every day is not only a learning process, but also a sharing process ๐Ÿ˜œ
  • ๐ŸŒฒ Tip: the problem-solving programming languages in this column are C# and Java
  • ๐ŸŒฒ To maintain a state of learning every day, let's work together to become the great God of algorithm ๐Ÿง!
  • ๐ŸŒฒ Today is the 97th day of punching out the force deduction algorithm ๐ŸŽˆ!
๐Ÿš€ Algorithm problem ๐Ÿš€

๐ŸŒฒ Sample of the original question: the most common words

Given a paragraph and a disabled word list. Returns the word that appears the most and is not in the disabled list.

The title ensures that at least one word is not in the disabled list, and the answer is unique.

Words in the disabled list are represented in lowercase letters without punctuation. Words in paragraphs are not case sensitive. The answers are all lowercase letters.

Example:

input: 
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
output: "ball"
explain: 
"hit" It appears 3 times, but it is a disabled word.
"ball" Twice (At the same time, no other words appear twice)๏ผŒTherefore, it is the word that appears most frequently in the paragraph and is not in the disabled list. 
Note that all these words are case insensitive in paragraphs, and punctuation needs to be ignored (even next to words, such as "ball,")๏ผŒ 
"hit"It is not the final answer. Although it appears more times, it is in the disabled word list.

Tips:

  • 1 < = paragraph length < = 1000
  • 0 < = number of disabled words < = 100
  • 1 < = disable word length < = 10
  • The answer is unique and all lowercase letters (even if it is capitalized in the paragraph, even some specific nouns, the answer is lowercase.)
  • The paragraph contains only letters, spaces and the following punctuation marks!? '
  • There are no words without hyphens or with hyphens.
  • Words contain only letters, no ellipsis or other punctuation marks.

๐ŸŒป C# method: Dictionary

Use the dictionary to process the data, and finally judge whether it belongs to prohibited words!

code:

public class Solution {
    public string MostCommonWord(string paragraph, string[] banned) {

            paragraph =paragraph.ToLower();
            string str = "";
            Dictionary<string, int> dic = new Dictionary<string, int>();
            for (int i = 0; i < paragraph.Length; i++)
            {
                if (paragraph[i]-'a'<0|| paragraph[i] -'a' >26)
                {
                    if (string.IsNullOrEmpty(str))
                    {
                        continue;
                    }
                    else
                    {
                        if (dic.ContainsKey(str))
                        {
                            dic[str]++;
                        }
                        else
                        {
                            dic.Add(str,1);
                        }
                        str = "";
                    }
                }
                else
                {
                    str += paragraph[i].ToString();
                }
            }
             if (str!=""&&dic.ContainsKey(str))
             {
                dic[str]++;
             }
                else
             {
                dic.Add(str,1);
             }
               str = "";            
            int num = int.MinValue;
            string res = "";
            foreach (var item in dic)
            {
                if (banned.Contains(item.Key))
                {
                    continue;
                }
                else
                {
                    if (item.Value>num)
                    {
                        num = item.Value;
                        res = item.Key;
                    }
                }
            }
            return res;
    }
}

results of enforcement

adopt
 Execution time: 112 ms๏ผŒAt all C# Beat 58.00% of users in submission
 Memory consumption: 39.9 MB๏ผŒAt all C# Beat 58.33% of users in submission

๐ŸŒป Java methods: simple counting

Train of thought analysis
We count the number of occurrences of each word and ignore all punctuation and case. The answer is the word that appears the most and is not in the disabled list.

There are two ways to count words. In the first method, we first split the whole paragraph according to the space, and then for each word, we remove the punctuation and ignore the case. In the second method, we scan the whole paragraph character by character. If we encounter a symbol that is not the mother of a word, we take the letter we encountered as a word.

For each word, we will put it into the hash map (HashMap in Java or Counter in Python) for counting. After each word is put in, if the word is not in the disabled list, we can update the answer once.

code:

class Solution {
    public String mostCommonWord(String paragraph, String[] banned) {
        paragraph += ".";

        Set<String> banset = new HashSet();
        for (String word: banned) banset.add(word);
        Map<String, Integer> count = new HashMap();

        String ans = "";
        int ansfreq = 0;

        StringBuilder word = new StringBuilder();
        for (char c: paragraph.toCharArray()) {
            if (Character.isLetter(c)) {
                word.append(Character.toLowerCase(c));
            } else if (word.length() > 0) {
                String finalword = word.toString();
                if (!banset.contains(finalword)) {
                    count.put(finalword, count.getOrDefault(finalword, 0) + 1);
                    if (count.get(finalword) > ansfreq) {
                        ans = finalword;
                        ansfreq = count.get(finalword);
                    }
                }
                word = new StringBuilder();
            }
        }

        return ans;
    }
}

results of enforcement

adopt
 Execution time: 5 ms๏ผŒAt all Java  Defeated 98 in submission.76%User
 Memory consumption: 38.2 MB๏ผŒAt all Java Defeated 88 in submission.29%User

Complexity analysis

Time complexity: O( P+B )
Space complexity: O(P+B) 

๐Ÿ’ฌ summary

  • Today is the 97th day of punching out the force deduction algorithm!
  • This paper uses C# and Java programming languages to solve problems
  • Some methods are also written by Likou God, and they are also shared while learning. Thanks again to the algorithm bosses
  • That's the end of today's algorithm sharing. See you tomorrow!

Topics: Java Algorithm leetcode