Back to DSA sheet

Naming a Company

HardArrays

Given distinct lowercase words, form a company name by picking two different words, swapping their first letters, and concatenating them with a space but only if both swapped words are not already in the original set. Count the number of valid ordered pairs.

Input format (stdin): the first line has n. Each of the next n lines is a word. Output the count of valid names.

Examples
Input: 4 coffee donuts time toffee
Output: 6
Six ordered pairs produce valid names.
Input: 3 lack back ack
Output: 4
Pairing the exclusive suffixes across first letters yields four valid names.
Constraints
  • 1 <= n <= 10^5
  • words are distinct lowercase
Sheets
NeetCode 250
naming-a-company.cpp2 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.