Text Justification
Hard About 40 min Free, no account needed
Greedily pack words into fully justified lines, spaces and all.
The brief
Given words and a line maxWidth, format the text fully justified:
- pack greedily: as many words per line as fit,
- distribute spaces evenly between words; when they do not divide evenly, the leftmost gaps get the extra space,
- the last line (and any line with a single word) is left-justified: words separated by one space, padded with spaces on the right,
- every output line is exactly
maxWidthcharacters.
The space arithmetic at the edges: a line with one word, the uneven-gap distribution, and the left-justified last line. The hidden tests target each.
Starter code
using System;
public class Solution
{
// TODO: return fully justified lines of exactly maxWidth characters.
public string[] FullJustify(string[] words, int maxWidth)
{
throw new NotImplementedException();
}
}How it is graded
Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.
What it practices
- String processing
- Parse, scan and match text character by character with correct edge cases.
- Greedy algorithms
- Make the locally best choice when an exchange argument proves it stays globally optimal.
Related katas
- Integer to Roman Medium Free Write Roman numerals the way Romans did - greedily.
- Fewest Cache Fetches Hard Free Use tomorrow's requests to find the smallest possible number of cache misses today.
- Magnetic Force Between Two Balls Hard Free Place m balls in baskets to maximize the smallest gap between any two.
- Boats to Save People Medium Free Pair people into two-person boats under a weight limit, using as few boats as possible.