Count Distinct Word Meanings
Updated
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.
More Duolingo interview questions
- Bouncing Square Reaches a Screen CornerOA · Seen Dec 2024
- Most-Read Page Across Every Valid StorylineOA · PHONE SCREEN · Seen Oct 2022
- Shortest Route Through a Traced MazeOA · Seen Sep 2022
- Rank Common Translation MistakesOA · Seen Sep 2022
- Count Legal Outfit CombinationsOA · Seen Sep 2021
- Group Timestamps into Consecutive-Day IntervalsPHONE SCREEN · Seen Nov 2026
- Return Every Matching DecryptionPHONE SCREEN · Seen Nov 2026
- Visit Desired Attractions Without Reusing a TrailPHONE SCREEN · Seen Oct 2026
- Correct an In-Place Array ReversalPHONE SCREEN · Seen Oct 2026
- Count Starts Reaching a Grid MaximumPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Record Linkage Part 3 - Full Connected ComponentStripe · PHONE SCREENStripe interview questions
- Sentence Equivalence with SynonymsRead AI · PHONE SCREEN
- Find Minimum City HopsAmazon · OA
- Shortest Path from Encoded Graph EdgesBitGo · OABitGo interview questions
- Department Headcounts and External FriendshipsIntuit · OAIntuit interview questions
- Disaster RecoveryHeadlands Technologies · OAHeadlands Technologies interview questions
- Numbers Station Message DecoderOptiver · OAOptiver interview questions
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions
- Evaluate Formulas with Cycle DetectionInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions