Sponsored
Sponsored
This approach involves using a hash map to count the frequency of each word in the paragraph after converting it to lowercase and removing punctuation. Then, the word with the highest count that is not in the banned list is selected as the result.
Time Complexity: O(N + M), where N is the length of the paragraph and M is the number of banned words. Space Complexity: O(N) for storing word frequencies.
1#include <iostream>
2#include <unordered_map>
3#include <unordered_set>
4#include <sstream>
5#include <algorithm>
6
7std::string mostCommonWord(std::string paragraph, std::vector<std::string>& banned) {
8 std::unordered_set<std::string> bannedSet(banned.begin(), banned.end());
9 std::unordered_map<std::string, int> wordFreq;
10 std::string word, result;
11 int maxCount = 0;
12
13 for (auto& c : paragraph) {
14 c = isalpha(c) ? tolower(c) : ' ';
15 }
16
17 std::istringstream iss(paragraph);
18
19 while (iss >> word) {
20 if (bannedSet.find(word) == bannedSet.end()) {
21 wordFreq[word]++;
22 if (wordFreq[word] > maxCount) {
23 maxCount = wordFreq[word];
24 result = word;
25 }
26 }
27 }
28
29 return result;
30}
31
32int main() {
33 std::vector<std::string> banned = {"hit"};
34 std::string paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.";
35 std::cout << mostCommonWord(paragraph, banned) << std::endl;
36 return 0;
37}
This C++ solution uses unordered_map
for word frequency counts and unordered_set
to check banned words efficiently. The istringstream
helps to split the paragraph into words, and non-alphabetic characters are replaced with spaces. The solution keeps track of the most frequent non-banned word.
This approach leverages advanced string manipulation functions available in each language for efficient parsing and counting. The words are extracted, normalized, and counted using advanced language-specific methods and libraries for cleaner code.
Time Complexity: O(N log N) due to sorting, where N is total words extracted. Space Complexity: O(N).
1
In this advanced C solution, we utilize qsort for sorting based on frequency after processing the paragraph. We tokenize the paragraph, convert to lowercase, check against banned set, and store in custom structure. The sorting provides the most frequent non-banned word.