Print Words Vertically - Problem
Print Words Vertically is a fascinating string manipulation problem that challenges you to think about text layout and matrix-like operations on strings.

Given a string s containing multiple words separated by spaces, your task is to arrange these words vertically in columns. Each word occupies its own column, and you read the result row by row.

For example, if you have "HOW ARE YOU", the words should be arranged like:
H A Y
O R O
W E U

The output should be ["HAY", "ORO", "WEU"].

Key Rules:
• Each word goes in its own column
• Read the result row by row to form output strings
• Add spaces when necessary to align shorter words
No trailing spaces allowed in the output strings
• Words appear in the same order as the input

Input & Output

example_1.py — Basic Case
$ Input: s = "HOW ARE YOU"
Output: ["HAY", "ORO", "WEU"]
💡 Note: We arrange the three words vertically: H-A-Y in first row, O-R-O in second row, W-E-U in third row. Each word forms a column, and we read horizontally to get the result.
example_2.py — Uneven Lengths
$ Input: s = "TO BE OR NOT TO BE"
Output: ["TBONTB", "OEROOE", " T NT ", " O "]
💡 Note: Words have different lengths. Shorter words are padded with spaces. The result shows how spaces are added to align words, but trailing spaces are removed from each result string.
example_3.py — Single Word
$ Input: s = "CONTEST"
Output: ["C", "O", "N", "T", "E", "S", "T"]
💡 Note: With only one word, each character becomes a separate row in the result. This is the edge case where we have only one column.

Constraints

  • 1 ≤ s.length ≤ 200
  • s contains only upper case English letters and spaces
  • There is exactly one space between consecutive words
  • Words contain only uppercase letters A-Z
  • The result must not have trailing spaces

Visualization

Tap to expand
Print Words Vertically - Visual ProcessInput: "HOW ARE YOU"HOW ARE YOUStep 1: Split into wordsHOWAREYOUStep 2: Arrange verticallyHOWAREYOUStep 3: Read horizontallyRow 0: HAYRow 1: ORORow 2: WEUFinal Output:["HAY", "ORO", "WEU"]⚡ Algorithm Insight• Process each word once• Build all result strings simultaneously• Use smart padding for alignment• Time: O(n×m), Space: O(n×m)where n = number of words, m = max length
Understanding the Visualization
1
Parse Input
Split 'HOW ARE YOU' into individual words
2
Determine Grid Size
Max word length (3) determines number of output rows
3
Fill Columns
Each word fills one column from top to bottom
4
Read Rows
Read horizontally to get final result strings
5
Clean Up
Remove trailing spaces from each result
Key Takeaway
🎯 Key Insight: Transform the problem from thinking about a 2D grid to directly building result strings. Each character gets placed exactly once in its correct position, making this an optimal O(n×m) solution.
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
24.0K Views
Medium Frequency
~15 min Avg. Time
890 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