Precompute the Suffix 2D array .
k = n - 2 NEW_LINE while ( k >= 0 ) : NEW_LINE INDENT for j in range ( MAX_CHAR ) : NEW_LINE INDENT r [ j ] [ k ] += r [ j ] [ k + 1 ] NEW_LINE DEDENT r [ ord ( s [ k ] ) - ord ( ' a ' ) ] [ k ] += 1 NEW_LINE k -= 1 NEW_LINE DEDENT