New 21 Game Solutions in C++
Number 837
Difficulty Medium
Acceptance 34.7%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/most-common-word/// Author : Hao Chen// Date : 2018-04-19class Solution {private:bool isLetter(char c) {return c >= 'a' && c <= 'z';}public:string mostCommonWord(string paragraph, vector<string>& banned) {unordered_map<string, int> banned_map, words_map;for (auto w:banned) {banned_map[w]++;}//conert the string to lower case.transform(paragraph.begin(), paragraph.end(), paragraph.begin(), ::tolower);//transfer the symbols to space.for (int i=0; i<paragraph.size(); i++) {if ( !isLetter(paragraph[i]) ){paragraph[i] = ' ';}}string word;for(auto c:paragraph) {if (isLetter(c)) {word += c;}else{if ( word.size()>0 ) {words_map[word]++;}word="";}}if ( word.size()>0 ) words_map[word]++;string result;int max_cnt=0;// go through the words_mapfor (auto const& w : words_map) {if ( banned_map.find(w.first) != banned_map.end() ) {continue;}if (max_cnt < w.second) {result = w.first;max_cnt = w.second;}}return result;}};