0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Leetcode Easy 819. Most Common Word

Last updated at Posted at 2020-01-05

解説
禁止文字に該当しない最大の出現回数である文字を返せば良い
-アルファベット以外の文字(!?.;など)を空白に変換する
-空白を区分字としてSplitする。(C++はSplitが無い。。。)
-文字は小文字に統一
-文字ごとに出現回数を記録する
-禁止文字に該当しない最大の出現回数である文字を返す

# include <algorithm>
# include <vector>
# include <random>
typedef long long  ll;
# define rep(i,s,n)for(ll i=s;i<n;i++)
# define repe(i,s,n)for(ll i=s;i<=n;i++)
# include <map>
# include <iostream>
class Solution {
public:
    map<string, int> mp;
    string mostCommonWord(string p, vector<string>& b) {
        string q;
        rep(i, 0, p.size()) {
            if (p[i] == '!' || p[i] == '?' || p[i] == '\'' || p[i] == ',' || p[i] == ';' || p[i] == '.') {
                p[i] = ' ';
            }
        }
        
        int s = 0;
        string t;
        rep(i, 0, p.size()) {
            if (p[i] != ' ') {
                if (p[i] >= 'A' && p[i] <= 'Z') {
                    //p[i] = (char)(p[i] + ('a' - 'A'));
                    p[i] = (char)(p[i] + ('a' - 'A'));
                }
                t += p[i];
            }
            else if(t != ""){
                mp[t]++;
                t = "";
            }
        }
        
        vector<pair<int,string>> v;
        for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) {
            v.push_back({ it->second, it->first });
        }
        sort(v.begin(), v.end());
        reverse(v.begin(), v.end());

        for (auto k : v) {
            bool isban = false;
            for (auto ban : b) {
                if (k.second == ban) {
                    isban = true;
                }
            }
            if (!isban) {
                return k.second;
            }
        }

        return t;
    }
};
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?