You're helping an innovative startup find the perfect company name using a creative naming process! πŸš€

Given an array of ideas containing potential name fragments, you need to count how many valid company names can be created using this special process:

  1. Choose two distinct ideas from the list (let's call them ideaA and ideaB)
  2. Swap their first letters - if ideaA = "coffee" and ideaB = "donuts", they become "doffee" and "conuts"
  3. Check validity - if both new words are NOT in the original ideas list, then "ideaA ideaB" (space-separated) is a valid company name
  4. Count all possibilities - return the total number of distinct valid company names

Example: With ideas ["coffee", "donuts", "time", "toffee"], swapping first letters of "coffee" and "time" gives us "toffee" and "cime". Since "toffee" exists in our original list, this combination is invalid. But "coffee" + "donuts" β†’ "doffee" + "conuts" works since neither appears in the original list!

Input & Output

example_1.py β€” Basic Example
$ Input: ideas = ["coffee", "donuts", "time", "toffee"]
β€Ί Output: 6
πŸ’‘ Note: Valid combinations: (coffee,donuts)β†’"doffee conuts", (donuts,coffee)β†’"conuts doffee", (coffee,time)β†’invalid (creates "toffee" which exists), (donuts,time)β†’"tonuts dime", (time,donuts)β†’"dime tonuts", (donuts,toffee)β†’"tonuts doffee", (toffee,donuts)β†’"doffee tonuts". Total valid names: 6
example_2.py β€” Simple Case
$ Input: ideas = ["lack", "back"]
β€Ί Output: 0
πŸ’‘ Note: Swapping 'lack' and 'back' gives 'back' and 'lack', both of which already exist in the original ideas array, so no valid company names can be formed.
example_3.py β€” All Different Letters
$ Input: ideas = ["aaa", "baa", "caa", "daa"]
β€Ί Output: 12
πŸ’‘ Note: All ideas have the same suffix 'aa' but different first letters. Any pair will create two new strings that don't exist in the original set. With 4 ideas, we have C(4,2) = 6 pairs, each contributing 2 valid names (both orderings), giving us 6 Γ— 2 = 12 total.

Constraints

  • 2 ≀ ideas.length ≀ 5 Γ— 104
  • 1 ≀ ideas[i].length ≀ 10
  • ideas[i] consists of lowercase English letters
  • All ideas[i] are distinct

Visualization

Tap to expand
Company Name Generation VisualizationStep 1: Group by LettercdStep 2: Find OverlapsCompare suffixesStep 3: Count Valid(n₁-overlap)Γ—(nβ‚‚-overlap)Γ—2Mathematical formulaStep 4: SumΞ£Add all resultsExample: ["coffee", "donuts", "time", "toffee"]Group 'c'coffee β†’ offeeGroup 'd'donuts β†’ onutsGroup 't'time β†’ imetoffee β†’ offeec ↔ d: (1-0) Γ— (1-0) Γ— 2 = 2c ↔ t: (1-1) Γ— (2-1) Γ— 2 = 0d ↔ t: (1-0) Γ— (2-0) Γ— 2 = 4Total: 2 + 0 + 4 = 6
Understanding the Visualization
1
Group Organization
Sort all word ideas into buckets based on their first letter, like organizing cards by suit
2
Suffix Analysis
For each pair of letter groups, identify which suffixes appear in both groups (these create invalid swaps)
3
Combination Counting
Calculate valid pairs using the formula: (group1_size - overlaps) Γ— (group2_size - overlaps) Γ— 2
4
Final Summation
Add up all valid combinations across all possible letter group pairs
Key Takeaway
🎯 Key Insight: Instead of checking every pair individually (O(n²)), group by first letters and use mathematical counting to calculate valid combinations in O(n + 26²) time!
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 25
43.7K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen