Solving Leetcode Interviews in Seconds with AI: Text Justification
Introduction
In this blog post, we will explore how to solve the LeetCode problem "68" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters. Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. For the last line of text, it should be left-justified, and no extra space is inserted between words. Note: A word is defined as a character sequence consisting of non-space characters only. Each word's length is guaranteed to be greater than 0 and not exceed maxWidth. The input array words contains at least one word. Example 1: Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16 Output: [ "This is an", "example of text", "justification. " ] Example 2: Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16 Output: [ "What must be", "acknowledgment ", "shall be " ] Explanation: Note that the last line is "shall be " instead of "shall be", because the last line must be left-justified instead of fully-justified. Note that the second line is also left-justified because it contains only one word. Example 3: Input: words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20 Output: [ "Science is what we", "understand well", "enough to explain to", "a computer. Art is", "everything else we", "do " ] Constraints: 1 <= words.length <= 300 1 <= words[i].length <= 20 words[i] consists of only English letters and symbols. 1 <= maxWidth <= 100 words[i].length <= maxWidth
Explanation
- Greedy Line Formation: Iterate through the words, packing as many as possible into each line without exceeding
maxWidth.- Space Distribution: For each line, calculate the number of spaces needed and distribute them evenly between words, with extra spaces going to the left for full justification (except for the last line).
- Left Justification of Last Line: Handle the last line separately, left-justifying it by adding spaces to the end.
- Runtime Complexity: O(N), where N is the number of words. Storage Complexity: O(N) due to the output list of formatted lines.
Code
def fullJustify(words, maxWidth):
result = []
current_line = []
current_length = 0
for word in words:
if current_length + len(word) + len(current_line) > maxWidth:
# Line is full, justify it
if len(current_line) == 1:
# Single word on the line, left-justify
result.append(current_line[0] + ' ' * (maxWidth - current_length))
else:
# Multiple words, full justify
spaces_needed = maxWidth - current_length
spaces_between = spaces_needed // (len(current_line) - 1)
extra_spaces = spaces_needed % (len(current_line) - 1)
line = ""
for i in range(len(current_line) - 1):
line += current_line[i]
line += ' ' * spaces_between
if extra_spaces > i:
line += ' '
line += current_line[-1]
result.append(line)
# Start a new line
current_line = [word]
current_length = len(word)
else:
# Add word to the current line
current_line.append(word)
current_length += len(word)
# Last line: left justify
last_line = ' '.join(current_line)
last_line += ' ' * (maxWidth - len(last_line))
result.append(last_line)
return result