Ctrl K

One New Letter

Medium
You are given two lists of words, base_words and goal_words. No word contains a repeated letter. A word from goal_words is reachable if it can be made from some word in base_words like this: 1. append one lowercase English letter that the word does not already contain; 2. rearrange the letters of the result in any order (keeping the order as it is also counts). The lists themselves never change: each word in goal_words is checked on its own. Return how many words in goal_words are reachable.
Example 1
base_words=["rat", "pen", "lo"], goal_words=["tarp", "pen", "owl", "open"] → 3

"tarp" is "rat" plus p, "owl" is "lo" plus w, and "open" is "pen" plus o, with letters rearranged. "pen" is already a base word, but a letter must be added, so it does not count. The answer is 3.

Example 2
base_words=["ab"], goal_words=["abcd", "b", "bca"] → 1

"abcd" is two letters longer and "b" is shorter. Only "bca" works. The answer is 1.

Example 3
base_words=["x", "yz"], goal_words=["zy", "xyz", "wx"] → 2
Constraints
  • 1 ≤ len(base_words), len(goal_words) ≤ 5 · 10⁴
  • 1 ≤ length of each word ≤ 26
  • Words consist of lowercase English letters
  • No letter repeats within a single word
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAnagram Buckets MediumA Subsequence of One Word Only MediumMost Searched Words MediumWord Grown Letter by Letter MediumMerge Contact Cards Medium
loading Python…
Input
base_words=["rat", "pen", "lo"], goal_words=["tarp", "pen", "owl", "open"]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 49 tests on the server, hidden ones included.