package com.fishercoder.solutions;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 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 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.
 1 <= 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 !?',;.
 Different words in paragraph are always separated by a space.
 There are no hyphens or hyphenated words.
 Words only consist of letters, never apostrophes or other punctuation symbols.
 */
public class _819 {
  public static class Solution1 {
    public String mostCommonWord(String paragraph, String[] banned) {
      Set<String> bannedSet = new HashSet(Arrays.asList(banned));
      String[] words = paragraph.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");
      Map<String, Integer> map = new HashMap<>();
      Arrays.stream(words)
          .filter(word -> !bannedSet.contains(word))
          .forEach(word -> map.put(word, map.getOrDefault(word, 0) + 1));
      String result = "";
      int freq = 0;
      for (String key : map.keySet()) {
        if (map.get(key) > freq) {
          result = key;
          freq = map.get(key);
        }
      }
      return result;
    }
  }
}

@@ -155,1491 +155,68 @@
 t;%0A%0A
-/**%0A * 819. Most Common Word%0A%0A Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words.%0A It is guaranteed there is at least one word that isn't banned, and that the answer is unique.%0A Words in the list of banned words are given in lowercase, and free of punctuation.%0A Words in the paragraph are not case sensitive.  The answer is in lowercase.%0A%0A Example:%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 Note:%0A%0A 1 %3C= paragraph.length %3C= 1000.%0A 1 %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 Different words in paragraph are always separated by a space.%0A There are no hyphens or hyphenated words.%0A Words only consist of letters, never apostrophes or other punctuation symbols.%0A */%0Apublic class _819 %7B%0A  public static class Solution1 %7B%0A
+public class _819 %7B%0A    public static class Solution1 %7B%0A    
     
@@ -277,24 +277,30 @@
 %5D banned) %7B%0A
+      
       Set%3CSt
@@ -355,24 +355,30 @@
 ed));%0A      
+  
+    
 String%5B%5D wor
@@ -450,16 +450,22 @@
 %5C%5Cs+%22);%0A
+      
       Ma
@@ -512,16 +512,22 @@
 ;%0A      
+  
+    
 Arrays.s
@@ -539,16 +539,26 @@
 (words)%0A
+          
         
@@ -602,16 +602,26 @@
 (word))%0A
+          
         
@@ -682,24 +682,30 @@
 , 0) + 1));%0A
+      
       String
@@ -718,16 +718,22 @@
 t = %22%22;%0A
+      
       in
@@ -744,16 +744,22 @@
 eq = 0;%0A
+      
       fo
@@ -798,16 +798,24 @@
         
+      
+  
 if (map.
@@ -829,24 +829,34 @@
 ) %3E freq) %7B%0A
+          
           re
@@ -867,16 +867,26 @@
  = key;%0A
+          
         
@@ -920,18 +920,32 @@
     
+  
-%7D%0A
+      %7D%0A      
       %7D%0A
     
@@ -936,24 +936,30 @@
           %7D%0A
+      
       return
@@ -971,16 +971,22 @@
 lt;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 819