LeetCode 58. Length of Last Word (LaTeX)

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.

If the last word does not exist, return 0.

Example

Example 1:

Input: "Hello World"
Output: 5

Example 2:

Input: "a "
Output: 1

Example 2:

Input: " "
Output: 0

Notes

A word is defined as a maximal substring consisting of non-space characters only.

Solution

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{datetime2}
\usepackage{expl3}

\begin{document}
\setlength{\parindent}{0cm}

\ExplSyntaxOn

\int_new:N \result
\str_new:N \g_tmpc_str

\cs_set:Npn \get_catcode:n #1 {
    \char_value_catcode:n { `#1 }
}

\cs_set:Npn \longest_last_word:n #1 {
    \str_gset:Nn \g_tmpa_str {#1}
    \par raw~input:~\str_use:N \g_tmpa_str \ (length:~\str_count:N \g_tmpa_str)
    % reverse the string, for convenience
    \tl_reverse:N \g_tmpa_str
    % trim spaces
    \str_gset:Nx \g_tmpb_str {\exp_args:NV \tl_trim_spaces:n \g_tmpa_str}
    % convert to string
    \par reversed~trimmed~input:~\str_use:N \g_tmpb_str \ (length:~\str_count:N \g_tmpb_str)
    \int_gset:Nn \result {0} % set result variable
    \str_if_empty:NTF {\g_tmpb_str} { }
        {
        \bool_gset:Nn \g_tmpa_bool \c_true_bool % loop variable
        \bool_do_while:Nn \g_tmpa_bool
            {
            \str_if_empty:NTF \g_tmpb_str
                {
                % exit loop if nothing is left
                \bool_gset:Nn \g_tmpa_bool \c_false_bool
                }
                {
                % get the head of string
                \str_gset:Nx \g_tmpc_str {\str_head:N \g_tmpb_str}
                % get catcode of string
                \exp_args:NNx \int_gset:Nn \g_tmpa_int { \exp_args:NV \get_catcode:n {\g_tmpc_str} }
                \par head: \g_tmpc_str \ (catcode~\int_use:N \g_tmpa_int)

                % check if head is space by comparing catcode
                \bool_if:nTF { \int_compare_p:nNn {\g_tmpa_int} {=} {10} ||
                                \int_compare_p:nNn {\g_tmpa_int} {=} {12} }
                    {% if head is space, exit loop
                    \bool_gset:Nn \g_tmpa_bool \c_false_bool
                    }
                    {% if it is not a space, increment counter
                    \int_gincr:N \result
                    % discard the head
                    \exp_args:NNx \str_gset:Nn \g_tmpa_str {\str_tail:N \g_tmpb_str}
                    \exp_args:NNV \str_gset:Nn \g_tmpb_str \g_tmpa_str
                    }
                }
            }

        }
}

\newcommand{\llw}[1]{\longest_last_word:n {#1}}

\ExplSyntaxOff


\llw{hello world}
\par \textbf{result: \the\result}
\llw{worlda}
\par \textbf{result: \the\result}
\llw{ }
\par \textbf{result: \the\result}
\llw{there is something special }
\par \textbf{result: \the\result}

\DTMnow

\end{document}

Output

raw input: hello world (length: 11)
reversed trimmed input: dlrow olleh (length: 11)
head:d (catcode 11)
head:l (catcode 11)
head:r (catcode 11)
head:o (catcode 11)
head:w (catcode 11)
head: (catcode 10)
result: 5
raw input: worlda (length: 6)
reversed trimmed input: adlrow (length: 6)
head:a (catcode 11)
head:d (catcode 11)
head:l (catcode 11)
head:r (catcode 11)
head:o (catcode 11)
head:w (catcode 11)
result: 6
raw input: (length: 1)
reversed trimmed input: (length: 0)
result: 0
raw input: there is something special (length: 27)
reversed trimmed input: laiceps gnihtemos si ereht (length: 26)
head:l (catcode 11)
head:a (catcode 11)
head:i (catcode 11)
head:c (catcode 11)
head:e (catcode 11)
head:p (catcode 11)
head:s (catcode 11)
head: (catcode 10)
result: 7
2020-06-10 19:03:11-04:00