Print Words Vertically - Problem

Given 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 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.

Input & Output

Example 1 — Basic Case
$ Input: s = "HOW ARE YOU"
Output: ["HAY","ORU","WE"]
💡 Note: Reading vertically: Column 0 = H+A+Y = "HAY", Column 1 = O+R+U = "ORU", Column 2 = W+E = "WE" (no trailing spaces)
Example 2 — Different Lengths
$ Input: s = "TO BE OR NOT TO BE"
Output: ["TBONTB","OEROOE"," T NT "," I "]
💡 Note: Words have varying lengths, shorter words contribute spaces. Column 2 has internal spaces but no trailing spaces.
Example 3 — Single Word
$ Input: s = "CONTEST"
Output: ["C","O","N","T","E","S","T"]
💡 Note: Single word becomes vertical - each character forms its own column

Constraints

  • 1 ≤ s.length ≤ 200
  • s contains only upper case English letters
  • It's guaranteed that there is only one space between 2 words

Visualization

Tap to expand
INPUTALGORITHMRESULTHOWAREYOUWords: ["HOW", "ARE", "YOU"]Max length: 31Split into words2Create columns by position3Collect characters vertically4Remove trailing spacesColumn 0: H + A + Y = "HAY"Column 1: O + R + U = "ORU"Column 2: W + E = "WE"["HAY", "ORU", "WE"]Column 0: HAYColumn 1: ORUColumn 2: WEVertical ReadingKey Insight:Think of words as matrix rows, then read each column vertically while handling variable word lengthsTutorialsPoint - Print Words Vertically | Matrix Column Processing
Asked in
Google 12 Amazon 8 Microsoft 6
18.5K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen