Modified run-length encoding (quite challenging one)