FastPrepPalindrome Match Percentage
Problem · String

Palindrome Match Percentage

Learn this problem
EasyApple logoAppleFULLTIMEONSITE INTERVIEW

Problem 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) → String

Examples

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.
  • text contains printable ASCII characters, including spaces and punctuation.
  • The returned value always contains exactly two digits after the decimal point.

More Apple problems

drafts saved locally
public String palindromeMatchPercentage(String text) {
    // Write your solution here.
}
text"racecar"
expected"100.00"
checking account