LeetCode 819. Most Common Word (LaTeX)

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

Example 1:

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.

Notes

Solution

\documentclass{article}
\usepackage[a3paper, landscape]{geometry}
\usepackage{newtxtext, newtxmath}
\usepackage{amsmath}
\usepackage{datetime2}
\usepackage{expl3}

\begin{document}

\ExplSyntaxOn

\cs_generate_variant:Nn \regex_replace_all:nnN {nVN}
\cs_generate_variant:Nn \regex_split:nnN {nVN}

\tl_new:N \l_tmpc_tl

\cs_new:Npn \__inline_a:nn #1#2 {
    ``#1''~(#2)\ \ %
    \int_compare:nNnTF {#2} > {\g_tmpa_int}  {
        \tl_gset:Nn \g_tmpa_tl {#1}
        \int_gset:Nn \g_tmpa_int {#2}
    } {}
}

\cs_new:Npn \most_common_word:n #1 {
    \tl_gset:Nx \g_tmpa_tl {\tl_lower_case:n {#1}}
    \str_gset:NV \g_tmpa_str \g_tmpa_tl
    \par lowercase~input:~\str_use:N \g_tmpa_str
    \regex_replace_all:nnN {\n|!|\?|'|,|;|\.} {\ } \g_tmpa_str
    \str_gset:Nx \g_tmpa_str {\g_tmpa_str}
    \par after~processing~(1):~\str_use:N \g_tmpa_str
    \regex_replace_all:nnN {\s{2,}} {\ } \g_tmpa_str
    \tl_gtrim_spaces:N \g_tmpa_str
    \par after~processing~(2):~\str_use:N \g_tmpa_str
    
    \regex_split:nVN {\s} \g_tmpa_str \g_tmpa_seq
    \prop_gclear:N \g_tmpa_prop
    \seq_map_variable:NNn \g_tmpa_seq \l_tmpa_tl {
        \prop_get:NVNTF \g_tmpa_prop \l_tmpa_tl \l_tmpb_tl {
            \tl_set:Nx \l_tmpc_tl {\int_eval:n {\l_tmpb_tl + 1}}
            \prop_gput:NVV \g_tmpa_prop \l_tmpa_tl \l_tmpc_tl
        } {
            \prop_gput:NVn \g_tmpa_prop \l_tmpa_tl {1}
        }
    }
    
    \int_gset:Nn \g_tmpa_int {0}
    \tl_gclear:N \g_tmpa_tl

    \par 
    \prop_map_function:NN \g_tmpa_prop \__inline_a:nn
}

\newcommand{\mcw}[1]{
    \most_common_word:n {#1}
    \par\textbf{most~common~word:~``\tl_use:N \g_tmpa_tl''~(\int_use:N \g_tmpa_int)}
}

\ExplSyntaxOff

\mcw{Bob hit a ball, the hit BALL flew far after it was hit.}
\mcw{a,b,b,b, ,, , b, a??}
\par\DTMNow

\end{document} 

Output

lowercase input: bob hit a ball, the hit ball flew far after it was hit.
after processing (1): bob hit a ball the hit ball flew far after it was hit
after processing (2): bob hit a ball the hit ball flew far after it was hit
“bob” (1) “hit” (3) “a” (1) “ball” (2) “the” (1) “flew” (1) “far” (1) “after” (1) “it” (1) “was” (1)
most common word: “hit” (3)
lowercase input: a,b,b,b, „ , b, a??
after processing (1): a b b b b a
after processing (2): a b b b b a
“a” (2) “b” (4)
most common word: “b” (4)
2020-06-18 12:00:44-04:00