๐ข 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!