An Approximation Algorithm for Space-Optimal Encoding of a Text