Count Ways to Build a Target from Increasing Dictionary Columns

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.

Visa logo
Visa
Oct 17, 2026
hardSoftware EngineerOnline AssessmentCoding & Algorithms
0
0

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 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 Guidance

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 Guidance

  • Why does ascending target-prefix iteration reuse the same column incorrectly?
  • What if the target is longer than every word?