FastPrepShortest Digit Prefix for Target Multisets
Problems/Shortest Digit Prefix for Target Multisets

Shortest Digit Prefix for Target Multisets

Visa logoVisa● MediumNEW GRADOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report identifies the source digit string, per-target shortest-prefix objective, and permutation-by-multiset interpretation. It omits impossible-target and excess-character behavior; this practice version returns -1 when impossible and permits unused prefix digits. The judged core task matches the visible report at about 85%.

Given a digit string digits and an array targets, return one answer per target.

The answer is the minimum prefix length of digits whose characters contain every digit of the target with at least the same multiplicity. Prefix characters that are not used by the target may be ignored, so the target may be rearranged into any permutation.

Return 0 for an empty target and -1 when the full source string lacks a required occurrence.

Function

shortestDigitPrefixes(digits: String, targets: String[]) → int[]

Examples

Example 1

digits = "012340221"targets = ["20","42","111","9"]return = [3,5,-1,-1]

Each answer is the latest required occurrence among that target's digits; the source has too few ones and no nine for the final targets.

Example 2

digits = "777"targets = ["","7","77","777"]return = [0,1,2,3]

The empty target needs no characters, while repeated sevens need successive occurrences.

Example 3

digits = "314159"targets = ["95","13","66"]return = [6,2,-1]

Excess prefix digits may remain unused.

Constraints

  • 0 <= digits.length <= 200000.
  • 1 <= targets.length <= 100000.
  • All strings contain only characters 0 through 9.
  • The combined target length is at most 200000.

More Visa problems

  • Count Prime Values in SubtreesOA · Seen Nov 2026
  • Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
  • House RobberPHONE SCREEN · Seen Nov 2026
All Visa interview questionsSee Visa hiring insights
Saved
class Solution {
    public int[] shortestDigitPrefixes(String digits, String[] targets) {
        // Write your solution here.
    }
}
Checking account…