Problem 206309 · medium · Phase 02 Linear Data Structures

Mirror Code Pairs

hash maps · strings · pair counting

Given a list of lowercase strings codes, count the index pairs (i, j) with i < j such that codes[j] is codes[i] written backwards. A palindrome is its own reverse, so two equal palindromes form a pair.

Examples

Input:  codes = ["ab", "ba", "cd", "dc", "ab"]
Output: 3
Explanation: (0,1), (2,3) and (1,4).

Input:  codes = ["aa", "aa", "aa"]
Output: 3

Constraints

  • 0 <= len(codes) <= 10**5, 1 <= len(codes[i]) <= 10
  • Target complexity: O(total characters) time.

Goals

  • Look up a transformed key (the reversed string)
  • Count pairs in one pass with a running table
Starting Python…