Ctrl K

Mirrored Codes

Easy
Every shelf in a warehouse is labeled with a two-letter code, and all codes in the list codes are distinct. Two codes form a **mirror pair** if one of them is the other read backwards (for example, "ab" and "ba"). Each code can be in at most one pair. Return the largest number of mirror pairs you can form.
Example 1
codes=['ko', 'pt', 'ok', 'mm', 'tp'] → 2

The pairs are "ko" with "ok" and "pt" with "tp". "mm" reads the same both ways, but there is no second copy, since all codes are distinct.

Example 2
codes=['ab', 'bc', 'ca'] → 0

No code is another one reversed.

Constraints
  • 1 ≤ len(codes) ≤ 50
  • len(codes[i]) = 2
  • codes[i] consists of lowercase English letters
  • All codes are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPump Rods at Their Peak HardOne-Row Typing EasyDinner for Two EasyPhrases with Synonyms EasyWord from a License Plate Easy
loading Python…
Input
codes=['ko', 'pt', 'ok', 'mm', 'tp']
Expected
2

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