We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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
`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Love-Letter Mystery
You are viewing a single comment's thread. Return to all 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]
`