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 to get "actk" then "tack". "act" cannot be formed from any start word by adding a letter (adding to "act" gives 4+ letters, adding to "ant" gives something with 4 letters that can't rearrange to "act"). "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
Count Words After Adding Letter INPUT startWords: "ant" "act" "tack" targetWords: "tack" "act" "acti" Bitmask Encoding: "ant" = bits at a,n,t "act" = bits at a,c,t "tack" = bits at a,c,k,t Each word encoded as 26-bit integer (char presence) ALGORITHM STEPS 1 Build HashSet Store bitmask of each startWord 2 For each targetWord Compute its bitmask 3 Try removing each char Toggle off each bit one by one 4 Check HashSet If result exists, count++ Example: "acti" Remove 'a': "cti" - not found Remove 'c': "ati" - not found Remove 't': "aci" - not found Remove 'i': "act" - FOUND! FINAL RESULT Target Analysis: "tack" Remove 'k' --> "act" in set OK "act" No start word +1 char = "act" X "acti" Remove 'i' --> "act" in set OK Output 2 Key Insight: Use bitmask to represent character presence in words. For each target word, try removing each character (toggle bit off) and check if the resulting mask exists in the startWords set. Time: O(n*26) for checking, Space: O(n) for HashSet. Rearrangement handled by bitmask! TutorialsPoint - Count Words Obtained After Adding a Letter | Optimal Solution (Bitmask + HashSet)
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