Quick Overview

Count target-string constructions by combining per-column character frequencies with prefix dynamic programming. Explain increasing-column constraints, descending updates, multiplicity, and modular arithmetic for larger inputs.

Count Ways to Build a Target from Increasing Dictionary Columns

Company: Visa

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Online Assessment

You are given equal-length words and a target string. Build the target from left to right. For each target character, choose a matching character from any word at a column strictly greater than the column used previously. Once a column is used, that column and all earlier columns are unavailable across every word. You may choose multiple characters from the same word if their columns increase. Return the number of construction choices modulo 1000000007. ### Constraints and Clarifications The report explicitly identifies [LeetCode 1639](https://leetcode.com/problems/number-of-ways-to-form-a-target-string-given-a-dictionary/description/) but says interview limits were much larger. Exact enlarged bounds are unknown; do not import the reference maxima. Use nonempty equal-length lowercase-English words and a nonempty lowercase target as the referenced domain. Different word indices supplying a matching character count as different choices, including identical word strings. ### Example For words ["aa","ab"] and target "aa", there are two choices at column 0 and one at column 1, so return 2. ### What a Strong Answer Covers Aggregate character frequencies by column, use dynamic programming over target prefixes, and prevent reuse of a column with a descending update order. Explain modulo arithmetic and the input-reading and DP costs separately. ### Follow-up Questions - Why does ascending target-prefix iteration reuse the same column incorrectly? - What if the target is longer than every word?

Overview: Count target-string constructions by combining per-column character frequencies with prefix dynamic programming. Explain increasing-column constraints, descending updates, multiplicity, and modular arithmetic for larger inputs.

You are given nonempty equal-length lowercase-English words and a nonempty lowercase-English target. Build the target from left to right. For each target character, choose a matching character from any word at a column strictly greater than the column used for the previous target character. Once a column is used, that column and all earlier columns are unavailable across every word. Multiple choices may come from the same word if their columns increase. Different word indices count as different choices, even when the word strings are identical. Implement countTargetConstructions(words, target) and return the number of choices modulo 1000000007. The report identifies the LeetCode 1639 domain but gives no exact enlarged interview size bounds; no reference maximum is assumed. Example: words = ["aa", "ab"], target = "aa" returns 2 because column zero offers two choices and column one offers one.

Constraints

  • words is nonempty, all words have the same nonzero length, and every character in words and target is lowercase English a through z.
  • target is nonempty; exact enlarged interview input-size bounds were not reported.
  • Columns chosen for successive target characters must be strictly increasing.
  • Different word indices are distinct choices, including identical word strings.
  • Return the count modulo 1000000007.

Examples

Input: (['aa', 'ab'], 'aa')

Expected Output: 2

Explanation: Two choices for the first a at column zero and one at column one.

Input: (['a'], 'a')

Expected Output: 1

Explanation: The only matching cell is one choice.

Hints

  1. A column can contribute several choices for one target character.
  2. A target longer than every word cannot use enough increasing columns.

Loading coding console...

Show the approach

Approach

Process columns from left to right. For each column, count how many word indices contain each letter there. Let dp[j] be the number of ways to form the first j target characters using columns processed so far, with dp[0] = 1. For j from min(column number, target length) down to 1, add dp[j-1] times the count of target[j-1] in this column, modulo 1000000007. Descending order ensures dp[j-1] still represents choices from earlier columns, so one column is never reused. Every valid construction has a unique final chosen column and word index, so the transition counts it once. A target longer than the word width keeps dp[target length] zero. JavaScript uses BigInt for multiplication before modulo; the bounded final residue converts exactly to Number. Other references use integer arithmetic wide enough for the per-column product in their input container.

Time complexity:
O(W*C + C*T), where W is word count, C is word length, and T is target length
Space complexity:
O(T + 26) auxiliary space