Two string problems for this week.
68. Text Justification
Given an array of words and a length L, format the text such that each line has exactly L 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 L characters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do 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.
For example, words:
["This", "is", "an", "example", "of", "text", "justification."]
L:16
.Return the formatted lines as:
[ "This is an", "example of text", "justification. " ]
my accepted code can be found here
Well this is more a engineering problem than an algorithm problem. So the solution is rather straightforward. We greedily select as many words in a line as long as the total length (including spaces) doesn’t exceed L
. And calculate the white spaces should be inserted into the words. And handle the special cases for 1. line containing only one word 2. last line needs no center justification and that’s it.
Justify(words: string[], l: int): string[]:
let result = empty string array
for words is not empty:
let linewords = SelectWordsForLine(words)
if words is empty:
append(result, JustifyLine([join(linewords, ' ')]))
else:
append(result, JustifyLine(linewords))
SelectWordsForLine(words: string[], l: int): string[]:
let result = slice(words, 0, j)
such that sum(length(s in result)) <= l
and sum(length(s in slice(words, 0, j + 1))) > l
truncate result from words
return result
JustifyLine(words: string[], l: int): string:
let totalLength = sum(length(word in words))
if length(words) == 1:
return word[0] padding with (l - totalLength) whitespaces
let n = (l - totalLength) / (length(words) - 1)
let extraCount = (l - totalLength) % (length(words) - 1)
let result = insert (n + 1) spaces between first extraCount words
n spaces between rest spaces
return result
Real pseudocode :P
Time complexity is O(n)
, n
being the total amount of characters.
132. Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s =
"aab"
, Return1
since the palindrome partitioning["aa","b"]
could be produced using 1 cut.
my accepted code can be found here
It’s a skillful dynamic programming problem.
First part of this problem is that we should recognize all palindromes in the string, which is a dynamic programming problem itself. For any pair of index i
, j
with i > j
, s[j to i]
is a palindrom if and only if
s[i] == s[j]
|i - j| <= 1
ors[j + 1 to i - 1]
is a palindrome
so the dp is like:
let palindrome = n * n boolean matrix with n = length(s)
fill palindrome with False
for i = 0 to n - 1:
for j = 0 to i:
if s[i] == s[j] and (i - j <= 1 or palindrome[i - 1][j + 1] is true
palindrome[i][j] = true
Next we see which partition of the string needs least cuts
let dp = int[length(s)]
for i = 0 to n - 1:
dp[i] = i
for j = 0 to i:
if palindrome[i][j] is true:
if j == 0 then dp[i] = 0
else dp[i] = min(dp[i], dp[j - 1] + 1)
result is dp[length(s) - 1]
dp[i]
is the number of minimum cuts of s[0 to i]
, and we calculate that from i = 0 to n - 1
.
For each i
, we first initialize the cell to i
, which is the number of cuts in worst case where there’s a cut between every character.
And then we look at every s[j to i]
, if j == 0
and s[0 to i]
is a palindrome, we don’t need a cut in the substring. Otherwise, the number of cut could be calculated by the number of cut in s[0 to j - 1]
plus the cut between j
and j - 1
given s[j to i]
is a palindrome, and we pick the minimum one.
Time and space are both in O(n^2)