Recipe Mirror Companion Totals
Count palindrome-forming concatenation partners for each string
A recipe app stores a short lowercase code for each recipe record. For a decorative sharing strip, it can place one record's code immediately before another record's code, without a separator.
A pair is mirror-ready when the resulting concatenation is a palindrome: it reads the same from left to right and right to left.
Given the array codes, return an integer array partners of the same length. For every index i, partners[i] must be the number of indices j such that:
j != i, andcodes[i] + codes[j]is a palindrome, withcodes[i]placed first.
Different records count separately even when their codes are identical. The same record cannot be paired with itself. A draft record may have an empty code; the empty string is a palindrome and concatenating it does not change the other code.
Return the counts in the original record order.
Examples
Example 1
Input: codes = ["ab","ba","a",""] Output: [2,1,2,1]
The record with code "ab" can be followed by "ba" to make "abba", or by "a" to make "aba". Order matters: placing "a" before "ab" does not make a palindrome.
Example 2
Input: codes = ["aa","aa","","b"] Output: [2,2,3,1]
The two records with code "aa" are separate possible companions for one another. The empty code can accompany any palindromic code, but its own record is still excluded from its companion count.
Example 3
Input: codes = ["abc","def"] Output: [0,0]
Neither ordering of the two distinct records produces a palindrome, and neither record may use itself.
Constraints
- 0 <= codes.length <= 100000
- 0 <= codes[i].length <= 30
- Each code contains only lowercase English letters
- The sum of all code lengths is at most 200000
Let S be the total number of characters, N the number of records, and L the maximum code length. The intended solution uses O(N + S*L) time and O(N + S) space, including the returned array. Since L is at most 30, direct palindrome checks on code fragments are sufficient; no specialized linear-time palindrome algorithm is required.
Hints
Show hint 1Hint 1
Store codes in reverse order in a trie. While reading a left-hand code forward, consider separately companions that end before it does and companions that continue after it ends.
Show hint 2Hint 2
At each trie node, count stored records whose unconsumed portion is a palindrome. Keep terminal multiplicities as well, and remove the current record's contribution exactly when its code is itself a palindrome.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you compute only the total number of unordered pairs for which at least one concatenation order is palindromic?
- How would you adapt the data structure to support inserting and removing records between companion-count queries?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem