Leetcode 2135: Count Words Obtained After Adding a Letter

grid47
grid47
Exploring patterns and algorithms
Apr 7, 2024 6 min read

Given two arrays of strings, startWords and targetWords, determine how many strings in targetWords can be formed by appending a letter to any string in startWords and rearranging the letters.
Problem
Approach
Steps
Complexity
Input: Two arrays of strings `startWords` and `targetWords`, each containing lowercase English letters.
Example: startWords = ["rat", "tap", "part"], targetWords = ["trap", "pat", "partz"]
Constraints:
• 1 <= startWords.length, targetWords.length <= 5 * 10^4
• 1 <= startWords[i].length, targetWords[j].length <= 26
• Each string consists of lowercase English letters.
Output: Return the number of strings in `targetWords` that can be formed by appending a letter to a string in `startWords` and rearranging the letters.
Example: Output = 2
Constraints:
• The result is a non-negative integer.
Goal: Identify the number of words in `targetWords` that can be formed from `startWords` using the conversion operation.
Steps:
• Convert each word in `startWords` and `targetWords` to bitmasks.
• For each word in `targetWords`, check if a matching word in `startWords` exists where only one additional letter is needed.
Goal: The input arrays and strings should adhere to the specified constraints for efficiency.
Steps:
• startWords and targetWords can each contain up to 50,000 strings.
• Each string has a maximum length of 26 characters.
Assumptions:
• The strings in both arrays consist of lowercase English letters only.
• No string in `startWords` or `targetWords` contains duplicate letters.
Input: startWords = ["rat", "tap", "part"], targetWords = ["trap", "pat", "partz"]
Explanation: From 'rat' we can form 'trap', from 'tap' we can form 'pat', but 'partz' cannot be formed.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus