Given a rows x cols screen and a sentence represented by a list of non-empty words, find how many times the given sentence can be fitted on the screen.
Note:
Example 1:
Input: sentence = ["hello", "world"] rows = 2 cols = 8 Output: 1 Explanation: hello--- world--- Character '-' signifies an empty space on the screen.
Example 2:
Input: sentence = ["a", "bcd", "e", "f"] rows = 3 cols = 6 Output: 2 Explanation: a-bcd- e-f-- a-bcd- Character '-' signifies an empty space on the screen.
Example 3:
Input: sentence = ["i", "had", "applepie"] rows = 4 cols = 5 Output: 1 Explanation: i-had apple pie-i had-- Character '-' signifies an empty space on the screen.
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 approach to fitting a sentence onto a screen means we try every possible way to arrange the words. We essentially simulate writing the sentence repeatedly onto the screen, counting how many times the entire sentence fits without going over the screen's boundaries.
Here's how the algorithm would work step-by-step:
def fitting_sentence_brute_force(sentence, rows, columns):
sentence_length = len(sentence)
sentence_string = ' '.join(sentence)
sentence_string_length = len(sentence_string)
sentence_index = 0
times_sentence_appears = 0
for row_index in range(rows):
current_column_length = 0
# Iterate until the current row is full.
while current_column_length + len(sentence[sentence_index % sentence_length]) <= columns:
word_length = len(sentence[sentence_index % sentence_length])
# Check if adding current word exceeds column limit.
if current_column_length + word_length <= columns:
current_column_length += word_length
# Increment sentence index and column length if a space is needed.
if current_column_length < columns:
current_column_length += 1
sentence_index += 1
# Count complete sentences.
if sentence_index >= sentence_length and sentence_index % sentence_length == 0:
times_sentence_appears += 1
else:
break
return times_sentence_appears
The key to solving this efficiently is to simulate how many times the sentence can fully fit on the screen before needing to consider partial repetitions. We pre-calculate some information about the sentence and screen dimensions to avoid redundant computations.
Here's how the algorithm would work step-by-step:
def sentence_screen_fitting(sentence, rows, columns):
combined_sentence = ' '.join(sentence) + ' '
sentence_length = len(combined_sentence)
characters_consumed = 0
for _ in range(rows):
characters_consumed += columns
# Adjust for spaces or partial words at the end of the row.
if combined_sentence[characters_consumed % sentence_length] == ' ':
characters_consumed += 1
elif combined_sentence[(characters_consumed % sentence_length) - 1] != ' ':
# Backtrack to the beginning of the last word.
while characters_consumed > 0 and combined_sentence[(characters_consumed % sentence_length) - 1] != ' ':
characters_consumed -= 1
# Calculate how many times sentence was fully repeated
full_repeats = characters_consumed // sentence_length
return full_repeats
Case | How to Handle |
---|---|
Empty sentence array | Return 0 immediately as no words exist to fit the screen. |
Empty row or cols | Return 0 if either rows or cols are zero because nothing can be fit. |
A single word longer than cols | Return 0 immediately, because the word cannot fit in a row. |
Sentence contains an empty string | Treat the empty string as a valid, albeit zero-length, word. |
Very large sentence size (memory implications) | The solution needs to avoid creating excessively large intermediate strings representing the entire sentence repeated many times to prevent memory issues, and instead use modular arithmetic |
Large rows and cols (potential integer overflow) | Ensure the calculations involving rows and cols don't lead to integer overflows; use long or appropriate data type if necessary. |
cols is large, but sentence can be fit many times | Optimize fitting multiple sentences on a row efficiently without iterating unnecessarily. |
Sentence with leading or trailing spaces in words | Trim the leading/trailing spaces of each word in the input sentence to ensure accurate fitting. |