LeetCode 1324. Print Words Vertically (LaTeX)
12 Jun 2020Given a string s. Return all the words vertically in the same order in which they appear in s.
Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).
Each word would be put on only one column and that in one column there will be only one word.
Example
Example 1:
Input: s = "HOW ARE YOU"
Output: ["HAY","ORO","WEU"]
Explanation: Each word is printed vertically.
"HAY"
"ORO"
"WEU"
Example 2:
Input: s = "TO BE OR NOT TO BE"
Output: ["TBONTB","OEROOE"," T"]
Explanation: Trailing spaces is not allowed.
"TBONTB"
"OEROOE"
" T"
Example 3:
Input: s = "CONTEST IS COMING"
Output: ["CIC","OSO","N M","T I","E N","S G","T"]
Notes
1 <= s.length <= 200scontains only upper case English letters.- It’s guaranteed that there is only one space between 2 words.
Solution
\documentclass{article}
\usepackage[a3paper, landscape]{geometry}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{datetime2}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\cs_new:Npn \print_vert:n #1 {
\seq_gclear:N \g_tmpa_seq
% store each word in the string into a sequence
\regex_split:nnN {\s} {#1} {\g_tmpa_seq}
\par seq:~\cs_meaning:N \g_tmpa_seq
% determine length of the longest word
\int_gset:Nn \g_tmpa_int {0}
\seq_map_variable:NNn \g_tmpa_seq \l_tmpa_tl {
\exp_args:NNx \int_set:Nn \l_tmpb_int {\str_count:N \l_tmpa_tl}
\int_gset:Nn \g_tmpa_int {\int_max:nn \l_tmpb_int \g_tmpa_int}
}
\par~longest~length:~\int_use:N \g_tmpa_int
\int_gset:Nn \g_tmpb_int {1} % loop var
% start printing output
\group_begin: \bfseries\ttfamily
% right-end spaces are not stripped for convenience!
\int_do_until:nNnn {\g_tmpb_int} {>} {\g_tmpa_int} {
\par\noindent
\seq_map_variable:NNn \g_tmpa_seq \l_tmpa_tl {
% get length of current string
\exp_args:NNx \int_set:Nn \l_tmpa_int {\str_count:N \l_tmpa_tl}
\int_compare:nNnTF {\g_tmpb_int} {>} {\l_tmpa_int} {
% if greater, output space
\space
}{
\str_item:Nn \l_tmpa_tl {\g_tmpb_int}
}
}
\int_gincr:N \g_tmpb_int
}
\group_end:
}
\newcommand{\printvert}[1]{\print_vert:n {#1}}
\ExplSyntaxOff
\printvert{HOW ARE YOU}
\printvert{TO BE OR NOT TO BE}
\printvert{CONTEST IS COMING}
\par\DTMNow
\end{document}
Output
seq: macro:->\s__seq \__seq_item:n {HOW}\__seq_item:n {ARE}\__seq_item:n {YOU}
longest length: 3
HAY
ORO
WEU
seq: macro:->\s__seq \__seq_item:n {TO}\__seq_item:n {BE}\__seq_item:n {OR}\__seq_item:n {NOT}\__seq_item:n {TO}\__seq_item:n {BE}
longest length: 3
TBONTB
OEROOE
T
seq: macro:->\s__seq \__seq_item:n {CONTEST}\__seq_item:n {IS}\__seq_item:n {COMING}
longest length: 7
CIC
OSO
N M
T I
E N
S G
T
2020-06-12 20:55:13-04:00