华为-2022-08-02-ai搜索岗一面二面

https://leetcode.cn/problems/most-common-word/

https://leetcode.cn/problems/the-number-of-the-smallest-unoccupied-chair/

原题

#include 
#include 
#include 
#include 

using namespace std;

int main()
{
    string paragraph;
    unordered_map wordCount;
    unordered_set banned;
    banned.insert("hit");
    // cin >> paragraph;
    paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.";

    int n = paragraph.length();
    int maxCount = 0;
    string maxCountString;

    for (int i = 0; i < n;)
    {
        int j = i;
        string word = "";
        while (j < n && isalpha(paragraph[j]))
        {
            // char c = (paragraph[j]>='A' && paragraph[j]<='Z') ? paragraph[j]+32:paragraph[j];
            // cout<<paragraph[j]<<", "<<c<<"\n";
            word.append(string(1,tolower(paragraph[j])));
            j++;
        }

        wordCount[word]++;

        while (j < n && !isalpha(paragraph[j]))
            j++;
        i = j;
    }

    for (auto &[word, cnt] : wordCount)
    {
        if (cnt > maxCount&&banned.count(word)==0)
        {
            maxCount = cnt;
            maxCountString = word;
        }
    }

    cout<<maxCountString<<"\n";

    return 0;
}
#华为面试##面试##秋招#
全部评论

相关推荐

点赞 4 评论
分享
牛客网
牛客企业服务