Practice/Amazon/Leetcode 68. Text Justification
CodingMust
You are given an array of words and a maximum line width. Your task is to format the text such that each line contains exactly maxWidth characters and is fully justified.
Pack as many words as possible into each line using a greedy approach: start with the first word and keep adding words until adding the next word would exceed maxWidth. Then format that line according to these rules:
maxWidth.Return an array of strings representing the formatted lines.
maxWidth charactersExample 1:
Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16 Output: ["This is an", "example of text", "justification. "] Explanation: Line 1: "This is an" = 10 chars, 6 spaces needed → distribute as 4-2 Line 2: "example of text" = 15 chars, 1 space needed → distribute evenly Line 3: Last line, left-justified with trailing spaces
Example 2:
Input: words = ["What", "must", "be", "acknowledgment", "shall", "be"], maxWidth = 16 Output: ["What must be", "acknowledgment ", "shall be "] Explanation: Line 1: Three words with 3 spaces distributed as 3-0 Line 2: Single word, left-justified Line 3: Last line, left-justified
Example 3:
Input: words = ["a"], maxWidth = 5 Output: ["a "] Explanation: Single word is left-justified with trailing spaces.