(Easy) Most Common Word - LeetCode

Description:

Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words.  It is guaranteed there is at least one word that isn‘t banned, and that the answer is unique.

Words in the list of banned words are given in lowercase, and free of punctuation.  Words in the paragraph are not case sensitive.  The answer is in lowercase.

Example:

Input:
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball"
Explanation:
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"),
and that "hit" isn‘t the answer even though it occurs more because it is banned.

Note:

  • 1 <= paragraph.length <= 1000.
  • 0 <= banned.length <= 100.
  • 1 <= banned[i].length <= 10.
  • The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.)
  • paragraph only consists of letters, spaces, or the punctuation symbols !?‘,;.
  • There are no hyphens or hyphenated words.
  • Words only consist of letters, never apostrophes or other punctuation symbols.

Accepted

73,213

Submissions

172,048

Solution:

Accepted

class Solution {
    public String mostCommonWord(String paragraph, String[] banned) {

       if(paragraph ==null||paragraph.length()==0){
           return "";
       }

        String[] res =  paragraph.toLowerCase().split("[ !?‘,;.]+"); //very important. 

        int max = -1;

        String result = "";

        for(int i = 0; i<res.length; i++){

           // System.out.println(" "+i+" "+res[i]+" "+Count(res,res[i]));

            if(Count(res, res[i])>max && Count(banned, res[i])==0){

                max = Count(res,res[i]);

                result = res[i];
               // System.out.println(" "+i+" "+result+" "+max);
                }
        }

        return result;

    }

    public static int Count(String[] arr, String s){

        int count = 0;
        for(int i = 0; i<arr.length; i++){
            if(arr[i].equals( s)){
                count++;
            }
        }

        return count;
    }
}

原文地址:https://www.cnblogs.com/codingyangmao/p/11458334.html

时间: 2024-10-13 11:55:38

(Easy) Most Common Word - LeetCode的相关文章

(Easy) Find Common Characters LeetCode

Description Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates).  For example, if a character occurs 3 times in all strings but not 4 times,

leetcode 819. Most Common Word

Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words.  It is guaranteed there is at least one word that isn't banned, and that the answer is unique. Words in the list of banned words are

Longest Common Prefix leetcode java

题目: Write a function to find the longest common prefix string amongst an array of strings. 题解: 解题思路是,先对整个String数组预处理一下,求一个最小长度(最长前缀肯定不能大于最小长度). 然后以第0个字符串作为参照,从第1个字符串到最后一个字符串,对同一位置做判断,有不同字符串返回当前记录的字符串就行. 我的代码如下,不是那么简洁好看,下面有个整理的更好一些: 1     private stat

Longest Common Prefix [LeetCode 14]

1- 问题描述 Write a function to find the longest common prefix string amongst an array of strings. 2- 思路分析 将数组内每个字串转换为List,每次批量取出各列表对应元素存入新列表,对新列表使用set去重.若set长度为1,说明元素一样,算入前缀. 3- Python实现 1 class Solution: 2 # @param {string[]} strs 3 # @return {string}

Longest Common Prefix -- leetcode

Write a function to find the longest common prefix string amongst an array of strings. 方法一,单个字符横向全体比较,纵向逐个的收集. class Solution { public: string longestCommonPrefix(vector<string> &strs) { if (!strs.size()) return ""; string prefix; for

(Easy) Occurences After Bigram LeetCode

Description: Given words first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second. For each such occurrence, add "third"

(Easy) Diet Plan Performance LeetCode Contest

Description: 5174. Diet Plan Performance My SubmissionsBack to Contest User Accepted:0 User Tried:0 Total Accepted:0 Total Submissions:0 Difficulty:Easy A dieter consumes calories[i] calories on the i-th day.  For every consecutive sequence of k days

Integer to English word leetcode java

问题描述: Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. For example, 123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundred Forty Five" 12345

819. Most Common Word

class Solution { public: string mostCommonWord(string paragraph, vector<string>& banned) { unordered_set<string> s(banned.begin(), banned.end()); unordered_map<string, int> m; int idx = 0; while (true) { string t = getLowerWord(parag