Class Solution
-
- All Implemented Interfaces:
public final class Solution2157 - Groups of Strings\.
Hard
You are given a 0-indexed array of strings
words. Each string consists of lowercase English letters only. No letter occurs more than once in any string ofwords.Two strings
s1ands2are said to be connected if the set of letters ofs2can be obtained from the set of letters ofs1by any one of the following operations:Adding exactly one letter to the set of the letters of
s1.Deleting exactly one letter from the set of the letters of
s1.Replacing exactly one letter from the set of the letters of
s1with any letter, including itself.
The array
wordscan be divided into one or more non-intersecting groups. A string belongs to a group if any one of the following is true:It is connected to at least one other string of the group.
It is the only string present in the group.
Note that the strings in
wordsshould be grouped in such a manner that a string belonging to a group cannot be connected to a string present in any other group. It can be proved that such an arrangement is always unique.Return an array
ansof size2where:ans[0]is the maximum number of groupswordscan be divided into, andans[1]is the size of the largest group.
Example 1:
Input: words = "a","b","ab","cde"
Output: 2,3
Explanation:
words0 can be used to obtain words1 (by replacing 'a' with 'b'), and words2 (by adding 'b'). So words0 is connected to words1 and words2.
words1 can be used to obtain words0 (by replacing 'b' with 'a'), and words2 (by adding 'a'). So words1 is connected to words0 and words2.
words2 can be used to obtain words0 (by deleting 'b'), and words1 (by deleting 'a'). So words2 is connected to words0 and words1.
words3 is not connected to any string in words.
Thus, words can be divided into 2 groups "a","b","ab" and "cde". The size of the largest group is 3.
Example 2:
Input: words = "a","ab","abc"
Output: 1,3
Explanation:
words0 is connected to words1.
words1 is connected to words0 and words2.
words2 is connected to words1.
Since all strings are connected to each other, they should be grouped together.
Thus, the size of the largest group is 3.
Constraints:
<code>1 <= words.length <= 2 * 10<sup>4</sup></code>
1 <= words[i].length <= 26words[i]consists of lowercase English letters only.No letter occurs more than once in
words[i].
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntArraygroupStrings(Array<String> words)-
-
Method Detail
-
groupStrings
final IntArray groupStrings(Array<String> words)
-
-
-
-