You can solve it with maximum two way match.
Each L character of a given string forms a left set. (Note that you are repeating characters if the string contains duplicate characters).
Each pair of characters (R1, R2) of the log forms the correct set.
L connects to (r1, r2) if L = R1 or L = R2.
Find the maximum match in the resulting graph. If all the left vertices are part of the match, you have the answer. If not, such a line is not possible.
See Maximum Two-way Matches for an algorithm.
Not sure if this is optimal, although sorry for not responding exactly as asked.
Aryabhatta
source share