A canvas-based chart draws tooltips itself, so it must break long text into lines. Each line may hold at most width characters.
Write wrapText(text, width) that returns the lines in order. Words are separated by one or more spaces. Put as many words as fit on each line, joined by single spaces, before starting the next line. A word longer than width goes on a line by itself and is not cut.
Lines never start or end with a space. If text has no words, return an empty array.
Input: text = "Revenue grew by twelve percent this quarter", width = 16 Output: ["Revenue grew by","twelve percent","this quarter"]
Revenue grew by is 15 characters; adding twelve would make 22.
Input: text = "Hi there", width = 20 Output: ["Hi there"]
Everything fits on one line.
0 <= text.length <= 10^41 <= width <= 200text contains letters, digits, punctuation, and spaces.Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #604 Wrap a Tooltip's Text. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.