Palindrome Match Percentage
Learn this problemProblem statement
Given a string text, measure how closely it matches a palindrome by comparing mirrored character pairs.
Let pairs = floor(text.length / 2). Count a pair when text[i] == text[text.length - 1 - i] for 0 <= i < pairs. The middle character of an odd-length string does not form a pair.
Return 100 × matchingPairs / pairs as a string with exactly two digits after the decimal point, rounded to the nearest hundredth with half values rounded up. Strings of length zero or one return "100.00".
Comparison is case-sensitive and literal: spaces and punctuation are not removed.
Function
palindromeMatchPercentage(text: String) → StringExamples
Example 1
text = "racecar"return = "100.00"All three mirrored pairs match. The middle e is excluded.
Example 2
text = "abca"return = "50.00"The outer a pair matches, while b and c do not.
Example 3
text = "abcdefa"return = "33.33"One of the three mirrored pairs matches, so the exact fraction is rounded to 33.33.
Constraints
0 <= text.length <= 20000.textcontains printable ASCII characters, including spaces and punctuation.- The returned value always contains exactly two digits after the decimal point.