Fit Words In Grid
MediumPython 3Strings
You are given a list of words, a number of rows num_rows, and a number of columns num_cols. The words are written out in order, cycling back to the first word after the last, and packed greedily into rows: within a row, words are separated by a single space and a word is placed only if it fits in the remaining columns; otherwise the row ends and the word starts the next row. Fill exactly num_rows rows. Return the total number of words written.
Constraints: - 1 <= num_rows - each word's length is at most num_cols (so it can fit on a line) - words is non-empty
Examples:
words = ["Do", "Run"], num_rows = 2, num_cols = 9 -> 5
Row 1: "Do Run Do" (2+1+3+1+2 = 9)
Row 2: "Run Do" ("Run" would overflow -> 10)
words = ["a"], num_rows = 3, num_cols = 1 -> 3 (one "a" per row)
words = ["ab", "cd"], num_rows = 1, num_cols = 2 -> 1 (only "ab" fits)
words = ["hello"], num_rows = 2, num_cols = 3 -> 0 (never fits)Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.
Loading the editor…