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 is put on only one column and in one column there will be only one word.

Input & Output

Example 1 — Basic Case
$ Input: s = "HOW ARE YOU"
Output: ["HAY","ORO","WEU"]
💡 Note: Split into words: ["HOW", "ARE", "YOU"]. Read vertically: H-A-Y forms first row, O-R-O forms second row, W-E-U forms third row.
Example 2 — Different Lengths
$ Input: s = "TO BE OR NOT TO BE"
Output: ["TBONTB","OEROOE"," T"]
💡 Note: Words have different lengths, so shorter words contribute spaces. Third row has trailing space removed.
Example 3 — Single Word
$ Input: s = "CONTEST"
Output: ["C","O","N","T","E","S","T"]
💡 Note: Single word becomes vertical list of its characters, one character per row.

Constraints

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

Visualization

Tap to expand
Print Words Vertically: Input → Output TransformationInput String:HOW ARE YOUArrange as Vertical Columns:HOWAREYOURead Rows Horizontally:HAYOROWEUFinal Output: ["HAY", "ORO", "WEU"]Each row becomes one string in the result array
Understanding the Visualization
1
Input
String with space-separated words: "HOW ARE YOU"
2
Arrange Vertically
Place words as columns, read rows horizontally
3
Output
Array of strings representing each row: ["HAY", "ORO", "WEU"]
Key Takeaway
🎯 Key Insight: Transform the problem into reading matrix rows after arranging words as columns
Asked in
Google 15 Microsoft 12 Amazon 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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