Ctrl K

Three-Letter Palindromes in a Word

Medium
You are given a word word. Pick three of its letters, keeping their order (they need not be adjacent), so that they form a palindrome: a string that reads the same forwards and backwards, such as "pop" or "eee". Return how many **distinct** three-letter palindromes can be formed this way. If the same palindrome can be picked in different ways, it still counts once.
Example 1
word="rotor" → 3

The matches are "ror", "rtr" and "oto". For instance, "ror" uses letters 0, 1 and 4.

Example 2
word="banana" → 3

"aaa", "ana" and "nan". The palindrome "ana" can be picked in several ways but counts only once.

Example 3
word="abc" → 0

All letters differ, so the outer letters of a palindrome can never match: the answer is 0.

Constraints
  • 3 ≤ len(word) ≤ 10⁵
  • word consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPalindrome After a Few Fixes MediumEven Vowel Stretch MediumAlmost Even Substrings MediumRecurring Gene Windows MediumSplit Into Equal-XOR Halves Medium
loading Python…
Input
word="rotor"
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 78 tests on the server, hidden ones included.