Count Words Obtained After Adding a Letter - Problem

You are given two 0-indexed arrays of strings startWords and targetWords. Each string consists of lowercase English letters only.

For each string in targetWords, check if it is possible to choose a string from startWords and perform a conversion operation on it to be equal to that from targetWords.

The conversion operation is described in the following two steps:

  1. Append any lowercase letter that is not present in the string to its end.
    • For example, if the string is "abc", the letters 'd', 'e', or 'y' can be added to it, but not 'a'. If 'd' is added, the resulting string will be "abcd".
  2. Rearrange the letters of the new string in any arbitrary order.
    • For example, "abcd" can be rearranged to "acbd", "bacd", "cbda", and so on. Note that it can also be rearranged to "abcd" itself.

Return the number of strings in targetWords that can be obtained by performing the operations on any string of startWords.

Note: You will only be verifying if the string in targetWords can be obtained from a string in startWords by performing the operations. The strings in startWords do not actually change during this process.

Input & Output

Example 1 — Basic Transformation
$ Input: startWords = ["ant","act","tack"], targetWords = ["tack","act","acti"]
Output: 2
💡 Note: "tack" can be formed from "act" by adding 'k' and rearranging. "act" can be formed from "act" by adding any missing letter then removing it (impossible), but "act" can be formed from "ant" by adding 'c' and rearranging. "acti" can be formed from "act" by adding 'i'. Total: 2 words can be transformed.
Example 2 — No Valid Transformations
$ Input: startWords = ["ab","a"], targetWords = ["abc","abcd"]
Output: 1
💡 Note: "abc" can be formed from "ab" by adding 'c'. "abcd" cannot be formed from any start word as it would require adding 2+ letters. Total: 1 word can be transformed.
Example 3 — Multiple Start Options
$ Input: startWords = ["g","go"], targetWords = ["og"]
Output: 1
💡 Note: "og" can be formed from "g" by adding 'o' and rearranging to get "go", then rearranging to "og". Total: 1 word can be transformed.

Constraints

  • 1 ≤ startWords.length, targetWords.length ≤ 5 × 104
  • 1 ≤ startWords[i].length, targetWords[i].length ≤ 10
  • startWords[i] and targetWords[i] consist of lowercase English letters only
  • No string in startWords is a prefix of any other string in startWords

Visualization

Tap to expand
Word Transformation: Add Letter + RearrangeStart Words"act""ant"Transformation1. Add missing letter2. Rearrange lettersTarget Words"tack""acti""act" + "k"→ "tack""ant" + "c" → "cant"rearrange → "act"✓ Can form"act" + "i" → "acti"✓ Can formResult: 2 words can be transformed
Understanding the Visualization
1
Input
Start words: ['ant', 'act'], Target words: ['tack', 'acti']
2
Transform
Add one missing letter + rearrange = new word
3
Count
Count how many targets can be formed
Key Takeaway
🎯 Key Insight: A target word can be formed if it has exactly one more unique character than any start word
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
15.2K Views
Medium Frequency
~25 min Avg. Time
428 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen