optimized Longest Common Prefix