Document Line Wrap

Asked at GoogleOnsite

Reported by the problem's creator, not verified

You are given the text of a document as a string text and a page width width in characters. The text holds words separated by spaces and "\n" characters.

Examples

  1. Example 1

    Input: text = Hello world Foo bar baz qux, width = 10

    Output: 4

    Explanation: Hello world is 11 characters, too wide for one row, so it wraps to two rows. Foo bar baz qux fits Foo bar and then baz qux, two more rows. Total 4.

  2. Example 2

    Input: text = a b, width = 5

    Output: 4

    Explanation: The text has four lines: a, two empty lines and b. Each line takes one row, including the empty ones.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.