3.4 Mobile - Text Wrap
(RankList for this Question)
Score: 10pts
Time Limit: 5.00 sec
You are given a string text and an integer lineWidth. Your task is to formats the text to fit within the specified line width while minimizing the number of line breaks, ensuring readability in mobile view.

The text may contain multiple words separated by spaces. You need to insert line breaks ('\n') in such a way that each line does not exceed the given lineWidth. If a word cannot fit within a line, it should be moved to the next line. However, you should avoid breaking words in the middle; instead, they should be moved to the next line entirely.

This formatting challenge is particularly important for ensuring a seamless reading experience on mobile devices with smaller screens. Write a function to solve this problem, and return a list of strings, where each string represents a line of text, making it easy to display on mobile screens.

Constraints
1 <= s <= 500

Input Format
The first line contains an sentence with spaces
Second line contains line Width

Output Format
list of string separated by new lines which represent all the wraps

Example 1
Input:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
20

Output:
Lorem ipsum dolor
sit amet,
consectetur
adipiscing elit.

Explanation:
Example is self explanatory

Log In to solve the Question