Print Words Vertically - Complete Solution Guide
Print Words Vertically is LeetCode problem 1324, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 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 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","OER
Detailed Explanation
The problem asks us to take a string `s` consisting of multiple words separated by single spaces, and print the words vertically. This means constructing a list of strings where each string represents a column formed by characters from the input words. If a word is shorter than the maximum length among all words, we pad the corresponding position in the resulting string with a space. Trailing spaces in the output strings should be removed.
Solution Approach
The solution involves splitting the input string into individual words, finding the maximum length among these words, and then iterating to build the vertical strings. For each vertical string, we iterate through the words. If the current index is within the bounds of a word, we append the character at that index; otherwise, we append a space. Finally, we remove any trailing spaces from each constructed vertical string.
Step-by-Step Algorithm
- Step 1: Split the input string `s` into a list of words using spaces as delimiters.
- Step 2: Determine the maximum length (`maxLen`) among all the words in the list.
- Step 3: Initialize an empty list `result` to store the vertically printed words.
- Step 4: Iterate from `i = 0` to `maxLen - 1` (inclusive), representing the rows (vertical strings).
- Step 5: Inside the outer loop, create a string builder `sb` to construct the i-th vertical string.
- Step 6: Iterate through each word in the list of words.
- Step 7: If the current row index `i` is within the length of the current word, append the character at index `i` to `sb`. Otherwise, append a space ' ' to `sb`.
- Step 8: After iterating through all words, remove any trailing spaces from the string builder `sb`. This is done by iterating backward from the end of the string and truncating it until a non-space character is encountered.
- Step 9: Convert the resulting string from the string builder `sb` to a string and add it to the `result` list.
- Step 10: Return the `result` list containing the vertically printed words.
Key Insights
- Insight 1: The core idea is to transpose the characters of the words. Each row in the input becomes a column in the output, and vice versa.
- Insight 2: The length of the longest word determines the number of rows in the output. This defines the iterations needed.
- Insight 3: Trailing spaces need to be removed from each output string to meet the problem requirements. This can be done efficiently using string manipulation techniques.
Complexity Analysis
Time Complexity: O(N*M)
Space Complexity: O(N*M)
Topics
This problem involves: Array, String, Simulation.
Companies
Asked at: Guidewire.