<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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;a&quot;,&quot;b&quot;,&quot;ab&quot;,&quot;cde&quot;]
<strong>Output:</strong> [2,3]
<strong>Explanation:</strong>
- words[0] can be used to obtain words[1] (by replacing &#39;a&#39; with &#39;b&#39;), and words[2] (by adding &#39;b&#39;). So words[0] is connected to words[1] and words[2].
- words[1] can be used to obtain words[0] (by replacing &#39;b&#39; with &#39;a&#39;), and words[2] (by adding &#39;a&#39;). So words[1] is connected to words[0] and words[2].
- words[2] can be used to obtain words[0] (by deleting &#39;b&#39;), and words[1] (by deleting &#39;a&#39;). So words[2] is connected to words[0] and words[1].
- words[3] is not connected to any string in words.
Thus, words can be divided into 2 groups [&quot;a&quot;,&quot;b&quot;,&quot;ab&quot;] and [&quot;cde&quot;]. The size of the largest group is 3.  
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;a&quot;,&quot;ab&quot;,&quot;abc&quot;]
<strong>Output:</strong> [1,3]
<strong>Explanation:</strong>
- words[0] is connected to words[1].
- words[1] is connected to words[0] and words[2].
- words[2] is connected to words[1].
Since all strings are connected to each other, they should be grouped together.
Thus, the size of the largest group is 3.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= words.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= words[i].length &lt;= 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>

class Solution:
    def groupStrings(self, words: List[str]) -> List[int]:
        # a -> ax, xa
        # a -> ''
        # a -> x
        # reversible / symmetric
        # there's only 1 unique division?
        # construct graph of edges
            # then dfs to find all the groups
            # graph of edges: naively will take O(n^2)
            # is there a trick to find all connected words in O(1)
                # abcdef
                    # connected to xbcdef, axcdef, ...
                    # n+1 words that have a prefix + suffix == s
                    # n-1 words that have a pref + suff of s
                        # eg bcdef -> == suff of s
                        # abdef -> pref of s + suff of s
                        # only have to check 1 of these directions
            # it's the set of letters...
                # so abc = bca
                    # then, check if removing any letter helps
                    # check if replacing any letter helps, but has to be done in sorted order
                    # sorted strings: have to resort during replacement O(26), but only have to try letters not in s
                # sorting can result in dupes, easiest resolution is tracking idxs?
                    # otherwise have to deal w counters
                        # basically incr size by c[w] instead of 1
        words = [''.join(sorted(w)) for w in words]
        # words = [frozenset(w) for w in words]
        cnt = Counter(words)
        deletes = defaultdict(list)
        # words = set(words)
        graph = defaultdict(list)
        # O(n)
        for w in cnt.keys():
            # for i, c in enumerate(w):
            # O(26)
            for i, c in enumerate(w):
                pref, suff = w[:i], w[i+1:]
                delete = pref + suff
                # delete = w.difference(c)
                if delete in cnt:
                    graph[w].append(delete)
                    graph[delete].append(w)
                for replace in deletes[delete]:
                # O(26)
                # can speed this up by just checking if delete == another word's delete
                # for c2 in ascii_lowercase:
                    # if c2 in w:
                        # continue
                    # replace = pref + c2 + suff
                    # O(26)
                    # replace = delete.union(c2)
                    #frozenset([c3 for c3 in w if c3 != c] + [c2])
                    # if replace in cnt:
                    graph[w].append(replace)
                    graph[replace].append(w)
                deletes[delete].append(w)
        # print(graph)
        seen = set()
        def dfs(w):
            if w in seen:
                # return []
                return 0
            seen.add(w)
            size = cnt[w]
            # members = [w] * cnt[w]
            for w2 in graph[w]:
                size += dfs(w2)
                # members.extend(dfs(w2))
            # return members
            return size

        groups = 0
        max_size = 0
        for w in cnt.keys():
            if w in seen:
                continue
            groups += 1
            d = dfs(w)
            # print(d)
            # max_size = max(max_size, len(d))
            max_size = max(max_size, d)
        return [groups, max_size]

71800