Class Solution
java.lang.Object
g2101_2200.s2157_groups_of_strings.Solution
2157 - Groups of Strings.<p>Hard</p>
<p>You are given a <strong>0-indexed</strong> array of strings <code>words</code>. Each string consists of <strong>lowercase English letters</strong> only. No letter occurs more than once in any string of <code>words</code>.</p>
<p>Two strings <code>s1</code> and <code>s2</code> are said to be <strong>connected</strong> if the set of letters of <code>s2</code> can be obtained from the set of letters of <code>s1</code> by any <strong>one</strong> of the following operations:</p>
<ul>
<li>Adding exactly one letter to the set of the letters of <code>s1</code>.</li>
<li>Deleting exactly one letter from the set of the letters of <code>s1</code>.</li>
<li>Replacing exactly one letter from the set of the letters of <code>s1</code> with any letter, <strong>including</strong> itself.</li>
</ul>
<p>The array <code>words</code> can be divided into one or more non-intersecting <strong>groups</strong>. A string belongs to a group if any <strong>one</strong> of the following is true:</p>
<ul>
<li>It is connected to <strong>at least one</strong> other string of the group.</li>
<li>It is the <strong>only</strong> string present in the group.</li>
</ul>
<p>Note that the strings in <code>words</code> should 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.</p>
<p>Return <em>an array</em> <code>ans</code> <em>of size</em> <code>2</code> <em>where:</em></p>
<ul>
<li><code>ans[0]</code> <em>is the <strong>maximum number</strong> of groups</em> <code>words</code> <em>can be divided into, and</em></li>
<li><code>ans[1]</code> <em>is the <strong>size of the largest</strong> group</em>.</li>
</ul>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> words = [“a”,“b”,“ab”,“cde”]</p>
<p><strong>Output:</strong> [2,3]</p>
<p><strong>Explanation:</strong></p>
<ul>
<li>
<p>words[0] can be used to obtain words[1] (by replacing ‘a’ with ‘b’), and words[2] (by adding ‘b’). So words[0] is connected to words[1] and words[2].</p>
</li>
<li>
<p>words[1] can be used to obtain words[0] (by replacing ‘b’ with ‘a’), and words[2] (by adding ‘a’). So words[1] is connected to words[0] and words[2].</p>
</li>
<li>
<p>words[2] can be used to obtain words[0] (by deleting ‘b’), and words[1] (by deleting ‘a’). So words[2] is connected to words[0] and words[1].</p>
</li>
<li>
<p>words[3] is not connected to any string in words.</p>
</li>
</ul>
<p>Thus, words can be divided into 2 groups [“a”,“b”,“ab”] and [“cde”]. The size of the largest group is 3.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> words = [“a”,“ab”,“abc”]</p>
<p><strong>Output:</strong> [1,3]</p>
<p><strong>Explanation:</strong></p>
<ul>
<li>
<p>words[0] is connected to words[1].</p>
</li>
<li>
<p>words[1] is connected to words[0] and words[2].</p>
</li>
<li>
<p>words[2] is connected to words[1].</p>
</li>
</ul>
<p>Since all strings are connected to each other, they should be grouped together.</p>
<p>Thus, the size of the largest group is 3.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 2 * 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 26</code></li>
<li><code>words[i]</code> consists of lowercase English letters only.</li>
<li>No letter occurs more than once in <code>words[i]</code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
groupStrings
-