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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code