Solve longest common prefix