Number of Prefix Connected Groups
You are given an array of strings words and an integer k.
Two words at distinct indices are considered prefix-connected if their first k characters are exactly identical. A connected group is a set of words where every pair of words is prefix-connected.
Return the total number of connected groups that contain at least two words.
Note:
- Words with a length strictly less than k cannot join any group and must be completely ignored.
- Duplicate strings in the array are treated as completely separate words.
Example 1:
Input: words = ["part", "park", "pasta", "pastry"], k = 3
Output: 2
Explanation:
"part" and "park" share the 3-character prefix "par", forming the first group. "pasta" and "pastry" share the prefix "pas", forming the second group. There are 2 connected groups with at least two words.
Example 2:
Input: words = ["same", "same", "same"], k = 2
Output: 1
Explanation:
Duplicate strings are treated as separate words. All three strings share the 2-character prefix "sa", forming 1 connected group of three words.
Now Your Turn!
Pick the correct output for the given inputInput: words = ["hello", "helium", "hero", "hi", "h"], k = 2
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= words.length <= 5000
- 1 <= words[i].length <= 100
- 1 <= k <= 100
- All strings in words consist only of lowercase English letters.