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","OEROOE"," T"] Explanation: Trailing spaces is not allowed. "TBONTB" "OEROOE" " T"
Example 3:
Input: s = "CONTEST IS COMING" Output: ["CIC","OSO","N M","T I","E N","S G","T"]
Constraints:
1 <= s.length <= 200
s
contains only upper case English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method tackles this problem by exploring all conceivable arrangements of the letters from the input words. Think of it like physically rearranging letter tiles until we find a valid vertical arrangement. We test each combination to see if it matches the rules.
Here's how the algorithm would work step-by-step:
def print_words_vertically_brute_force(input_string):
words = input_string.split()
length_of_longest_word = 0
for word in words:
length_of_longest_word = max(length_of_longest_word, len(word))
vertical_words = ["" for _ in range(length_of_longest_word)]
# Iterate through each column (original word)
for word in words:
#Iterate through each row (character in the current word)
for index_of_character, character in enumerate(word):
# Building vertical word by appending char
vertical_words[index_of_character] += character
# Pad the rest of the vertical words with spaces
for index_of_vertical_word in range(len(word), length_of_longest_word):
vertical_words[index_of_vertical_word] += ' '
result = []
# Remove trailing spaces
for vertical_word in vertical_words:
result.append(vertical_word.rstrip())
return result
The goal is to print words vertically, so think about reading each word one letter at a time, top to bottom. The key is to find the length of the longest word to know how many rows to create. We build each row by picking the corresponding letter from each word, or adding a space if the word is too short.
Here's how the algorithm would work step-by-step:
def print_words_vertically(input_string):
words = input_string.split()
longest_word_length = 0
for word in words:
longest_word_length = max(longest_word_length, len(word))
result = []
# Iterate up to the length of the longest word.
for i in range(longest_word_length):
line = ""
for word in words:
# Build each line by picking the ith char.
if i < len(word):
line += word[i]
else:
line += " "
# Remove trailing spaces to match requirements.
result.append(line.rstrip())
return result
Case | How to Handle |
---|---|
Null or empty input string | Return an empty list if the input string is null or empty to avoid null pointer exceptions and handle trivial cases. |
Input string with only spaces | Split the string and check if the resulting array is empty or contains only empty strings, returning an empty list. |
Input string with a single word | The solution should still correctly process the single word and return it as a single-element list. |
Words with varying lengths, including very short and very long words | The solution must correctly pad shorter words with spaces to the length of the longest word to maintain alignment. |
Input with leading or trailing spaces in the words | Trim leading/trailing spaces from each word to prevent incorrect padding or alignment issues. |
Words containing non-alphanumeric characters (e.g., punctuation) | The solution should process all characters in the words without errors, treating them as part of the vertical strings. |
Very large input string leading to memory constraints | Allocate memory efficiently and consider using a StringBuilder or similar approach to minimize memory usage when constructing the output strings. |
All words in the input string are the same length | The solution handles this scenario correctly since it iterates to the maximum length and no additional padding is needed. |