Count Distinct Word Meanings
Problem statement
You are given an array words of lowercase English strings.
Two different strings are directly related when their lengths differ by exactly 1 and deleting exactly one character from the longer string produces the shorter string. This relationship is undirected.
Two strings have the same meaning when they are identical or connected by a chain of directly related strings that appear in words. Repeated occurrences of the same string belong to one meaning and do not create additional groups.
Return the number of distinct meanings represented by words.
Function
countDistinctMeanings(words: String[]) → intExamples
Example 1
words = ["caw","caaw","caww","hoot","hooot","chirp"]return = 3The groups are [caw, caaw, caww], [hoot, hooot], and [chirp]. Both four-letter words connect to caw after one deletion.
Example 2
words = ["abc","abd","abcd"]return = 1Deleting d from abcd produces abc, while deleting c produces abd. The shared longer word connects all three strings into one meaning.
Constraints
1 <= words.length <= 20000.1 <= words[i].length <= 20.- Every string contains only lowercase English letters.
- The sum of all input-string lengths is at most
200000. - Repeated identical strings are allowed.