↓Skip to main content
  1. LeetCode/

LeetCode 3839: Number of Prefix Connected Groups

·1 min· ·
LeetCode Medium Hash-Table String Counting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-02-14

  • Submissions: Wrong Answer → Accepted

Approach
#

“Same first k characters” is an equivalence relation, so words sharing a prefix form one group, no Union-Find needed. Count by word[:k]; each key with at least two words is a group (Hash Table Template 1).

My Attempts
#

Attempt 1 did not exclude words shorter than k: Python’s word[:k] does not fail, it returns the whole word. For ["b", "b"], k = 2 both words are too short, so the answer is 0; it grouped the two "b"s and returned 1. Adding len(g) == k passed.

Solution
#

class Solution:
    def prefixConnected(self, words: List[str], k: int) -> int:
        cnt = Counter(w[:k] for w in words if len(w) >= k)
        return sum(v >= 2 for v in cnt.values())

Complexity
#

Time O(n · k), space O(n · k).

Mistakes to Avoid
#

  • Slicing past the end does not raise; exclude short words yourself.

Interview Explanation
#

Sharing the first k characters connects words, and it is an equivalence relation, so each prefix is a group. I count by word[:k], skip words that are too short, and count prefixes appearing at least twice. Time O(nk).

Related

LeetCode 2131: Longest Palindrome by Concatenating Two Letter Words
·2 mins
LeetCode Medium Greedy Hash-Table String Counting
LeetCode 2131 note: pair each word with its reverse on both sides; self-palindromes also pair with each other, and only one leftover can sit in the middle
LeetCode 3805: Count Caesar Cipher Pairs
·2 mins
LeetCode Medium Math Hash-Table String Counting
LeetCode 3805 note: shift each string so its first letter is a, making Caesar-equivalent strings identical, then count pairs per normalized form
LeetCode 244: Shortest Word Distance II
·2 mins
LeetCode Medium Two-Pointers Design Hash-Table String Array
LeetCode 244 note: precompute each word’s positions (sorted by construction), then answer each query by merging the two position lists with two pointers, always advancing the smaller one
LeetCode 923: 3Sum With Multiplicity
·4 mins
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 note: sort, fix the first number, and find the other two with converging pointers; equal end values are counted with a combination, different ones by multiplying each end’s duplicate count
LeetCode 3306: Count of Substrings Containing Every Vowel and K Consonants II
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 3306 note: “exactly k consonants” is not monotonic; compute “all five vowels and at least k consonants” minus “at least k + 1”, both “at least” counting windows
LeetCode 1358: Number of Substrings Containing All Three Characters
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 1358 note: an “at least one each of a, b, c” counting window: keep shrinking while all three are present, then count += left