The Love-Letter Mystery

  • + 0 comments

    Step 1: Convert each character in the string to its ASCII value using ord(). Step 2: Use two pointers (st and en) to compare characters from the start and end of the string. Step 3: For each mismatch, calculate the difference in ASCII values and add it to the count. Step 4: Return the total number of operations needed to make the string a palindrome.

    def theLoveLetterMystery(s): # Convert each character to its ASCII value res = [ord(char) for char in s]

    # Initialize pointers and operation counter
    st = 0
    en = len(res) - 1
    count = 0
    
    # Compare characters from both ends
    while st < en:
        count += abs(res[st] - res[en])
        st += 1
        en -= 1
    
    return count
    

    `