Rearranged the characters in pairs for better performance but still the program is not efficient enough