FastPrepCount Distinct Word Meanings

Count Distinct Word Meanings

Duolingo logoDuolingo● MediumINTERNOA
Learn

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[]) → int

Examples

Example 1

words = ["caw","caaw","caww","hoot","hooot","chirp"]return = 3

The 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 = 1

Deleting 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.

More Duolingo problems

See Duolingo hiring insights
public int countDistinctMeanings(String[] words) {
    // Write your code here.
}
words["caw","caaw","caww","hoot","hooot","chirp"]
expected3
Checking account…