@@ -0,0 +1,3154 @@
+package string;%0A%0Aimport java.util.HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0Apublic class Q819MostCommonWord %7B%0A%0A    //Difficulty: easy%0A    //TAG: Amazon%0A    //TAG: string%0A    //TAG: map%0A%0A    /**%0A     * 819. Most Common Word%0A     * 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.%0A     *%0A     * 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.%0A     *%0A     *%0A     *%0A     * Example:%0A     *%0A     * Input:%0A     * paragraph = %22Bob hit a ball, the hit BALL flew far after it was hit.%22%0A     * banned = %5B%22hit%22%5D%0A     * Output: %22ball%22%0A     * Explanation:%0A     * %22hit%22 occurs 3 times, but it is a banned word.%0A     * %22ball%22 occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.%0A     * Note that words in the paragraph are not case sensitive,%0A     * that punctuation is ignored (even if adjacent to words, such as %22ball,%22),%0A     * and that %22hit%22 isn't the answer even though it occurs more because it is banned.%0A     *%0A     *%0A     * Note:%0A     *%0A     * 1 %3C= paragraph.length %3C= 1000.%0A     * 0 %3C= banned.length %3C= 100.%0A     * 1 %3C= banned%5Bi%5D.length %3C= 10.%0A     * 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.)%0A     * paragraph only consists of letters, spaces, or the punctuation symbols !?',;.%0A     * There are no hyphens or hyphenated words.%0A     * Words only consist of letters, never apostrophes or other punctuation symbols.%0A     */%0A%0A    /*%0A    Solution:%0A%0A    1. convert banned to set for fast search%0A    2. count words not in banned set in map, keep update max count and related word%0A%0A    return final word%0A%0A    Time: O(m + n)%0A    Space: O(m + n)%0A     */%0A%0A    public String mostCommonWord(String paragraph, String%5B%5D banned) %7B%0A        Set%3CString%3E set = new HashSet%3C%3E();%0A        for (String str: banned) set.add(str);%0A        paragraph = paragraph.toLowerCase();%0A        Map%3CString, Integer%3E map = new HashMap%3C%3E();%0A        int start = -1, count = 0;%0A        String res = %22%22;%0A        for (int i = 0; i %3C paragraph.length(); i++) %7B%0A            char c = paragraph.charAt(i);%0A            if (isLetter(c)) %7B%0A                if (start == -1) start = i;%0A                if (i == paragraph.length() - 1 %7C%7C !isLetter(paragraph.charAt(i + 1))) %7B%0A                    String word = paragraph.substring(start, i + 1);%0A                    if (!set.contains(word)) %7B%0A                        map.put(word, map.getOrDefault(word, 0) + 1);%0A                        if (count %3C map.get(word)) %7B%0A                            count = map.get(word);%0A                            res = word;%0A                        %7D%0A                    %7D%0A                    start = -1;%0A                %7D%0A            %7D%0A        %7D%0A        return res;%0A    %7D%0A%0A    private boolean isLetter(char c) %7B%0A        return c - 'a' %3E= 0 && c - 'a' %3C 26;%0A    %7D%0A%0A%7D%0A

819. Most Common Word