BackeasyTrieGoogleAmazon

Network Network Tracker 11 Solution

Problem Statement

You are given a list of distinct strings ids representing network identifiers and a target string target representing a concatenated metric sequence. Each identifier may be used at most once. Determine the largest possible number of identifiers that can be concatenated, in any order, to form exactly the string target. If no subset of identifiers can produce target, return -1.

To solve the problem efficiently, first insert all identifiers into a Trie. Then perform a depth‑first search (DFS) on the target string: at each position, walk down the Trie following the characters of target to discover all identifiers that match a prefix starting at the current index. For every matching identifier, recursively continue the search from the index after the matched prefix, marking the identifier as used. The recursion explores all feasible selections; the maximum depth reached corresponds to the maximum count of identifiers. The algorithm must backtrack correctly when a branch does not lead to a full match.

Input: The first line contains an integer n (1 ≤ n ≤ 20) – the number of identifiers. The next n lines each contain a non‑empty string ids[i] consisting of lowercase English letters. The last line contains the string target (1 ≤ |target| ≤ 200). All strings have length ≤ 20.

Output: A single integer – the maximum number of identifiers that can be concatenated to obtain target, or -1 if it is impossible.

Example 1
Input
5 net work tracker networ ktrack networktracker
Output
3

Explanation: The target "networktracker" can be built by concatenating "net" + "work" + "tracker". This uses three identifiers, which is the largest possible count. Other combinations such as "networ" + "ktrack" + "er" are invalid because "er" is not an identifier.

Example 2
Input
5 ab abc cd def abcd abcd
Output
2

Explanation: Two ways exist: using the single identifier "abcd" (count 1) or using "ab" followed by "cd" (count 2). The maximum count is 2, so the answer is 2.

Example 3
Input
2 a b c
Output
-1

Explanation: Neither "a" nor "b" (or any combination) can produce the target string "c". Hence it is impossible and the result is -1.

Constraints

  • 1 <= n <= 20
  • 1 <= |ids[i]| <= 20
  • 1 <= |target| <= 200
  • All strings consist only of lowercase English letters
  • All identifiers in ids are distinct
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Network Network Tracker 11 — Problem Statement & Solution Guide

TrieEasyRecursive Backtracking
TimeO(N * L^2)
|
SpaceO(N * L)

Problem Description

You are given a list of distinct strings ids representing network identifiers and a target string target representing a concatenated metric sequence. Each identifier may be used at most once. Determine the largest possible number of identifiers that can be concatenated, in any order, to form exactly the string target. If no subset of identifiers can produce target, return -1.

To solve the problem efficiently, first insert all identifiers into a Trie. Then perform a depth‑first search (DFS) on the target string: at each position, walk down the Trie following the characters of target to discover all identifiers that match a prefix starting at the current index. For every matching identifier, recursively continue the search from the index after the matched prefix, marking the identifier as used. The recursion explores all feasible selections; the maximum depth reached corresponds to the maximum count of identifiers. The algorithm must backtrack correctly when a branch does not lead to a full match.

Input: The first line contains an integer n (1 ≤ n ≤ 20) – the number of identifiers. The next n lines each contain a non‑empty string ids[i] consisting of lowercase English letters. The last line contains the string target (1 ≤ |target| ≤ 200). All strings have length ≤ 20.

Output: A single integer – the maximum number of identifiers that can be concatenated to obtain target, or -1 if it is impossible.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Network Network Tracker 11"

easy

WHY DOES IT MATTER?

This pattern is essential for problems involving string matching and optimization where the order of elements matters but the total count is the objective. It combines string processing with dynamic programming to achieve an efficient solution.

OPTIMIZATION CHALLENGE

The key optimization is using a Trie to quickly identify valid identifiers within the target string, reducing the time complexity of the inner loop of the DP transition.

REAL-WORLD CONNECTION

This is analogous to parsing a log file where you need to identify the maximum number of distinct error codes that can be found in a sequence of log entries. It is also similar to tokenizing a string into the maximum number of valid tokens from a given vocabulary.

In an interview, clearly articulate the trade-off between using a Trie for fast prefix matching and the complexity of tracking used identifiers. If the number of identifiers is small, a bitmask DP is feasible; if large, assume that the 'at most once' constraint is naturally satisfied by the distinctness of the identifiers in the optimal solution.

COMPLEXITY AT A GLANCE

⏱ Time:O(N * L^2)
💾 Space:O(N * L)

Core Theory — Why This Approach?

The problem requires finding the maximum number of distinct strings from a list that can be concatenated to form a target string. A naive backtracking approach would involve generating all permutations of the input list and checking if any permutation's concatenation matches the target, which is computationally infeasible for large inputs due to factorial time complexity. The key insight is that the order of concatenation does not matter for the final string equality if we treat the problem as a multiset matching problem, but since the strings are distinct and must be used in a specific sequence to form the target, we must consider the positions where each identifier can fit within the target.

Interview Questions on This Problem

Q1How would you optimize the search for valid identifiers within the target string to avoid redundant checks?

Use a Trie data structure to store all identifiers. This allows for efficient prefix matching. When traversing the target string, you can quickly determine if a substring starting at any index is a valid identifier by walking down the Trie. This reduces the time complexity of checking for valid identifiers from O(N * L) to O(L) per position, where L is the length of the target.

Q2What is the role of dynamic programming in solving this problem, and how do you define the state?

Dynamic programming is used to find the maximum number of identifiers that can be used to form a prefix of the target string. The state can be defined as dp[i], which represents the maximum number of identifiers that can be concatenated to form the substring target[0..i-1]. The transition involves checking all possible identifiers that end at index i and updating dp[i] based on dp[j] where j is the start index of the identifier.

Q3How do you handle the constraint that each identifier can be used at most once?

Since the identifiers are distinct and we are looking for the maximum count, we can use a bitmask or a set to track which identifiers have been used. However, if the number of identifiers is large, this can become infeasible. In practice, if the identifiers are short and the target is long, we can assume that the same identifier won't be needed multiple times in a way that conflicts with the 'at most once' constraint, or we can use a more advanced DP state that includes the set of used identifiers, though this is typically only feasible for small N.

Examples

Example 1

Input

5
net
work
tracker
networ
ktrack
networktracker

Output

3

Explanation: The target "networktracker" can be built by concatenating "net" + "work" + "tracker". This uses three identifiers, which is the largest possible count. Other combinations such as "networ" + "ktrack" + "er" are invalid because "er" is not an identifier.

Example 2

Input

5
ab
abc
cd
def
abcd
abcd

Output

2

Explanation: Two ways exist: using the single identifier "abcd" (count 1) or using "ab" followed by "cd" (count 2). The maximum count is 2, so the answer is 2.

Example 3

Input

2
a
b
c

Output

-1

Explanation: Neither "a" nor "b" (or any combination) can produce the target string "c". Hence it is impossible and the result is -1.

Constraints

  • 1 <= n <= 20
  • 1 <= |ids[i]| <= 20
  • 1 <= |target| <= 200
  • All strings consist only of lowercase English letters
  • All identifiers in ids are distinct

Optimal Approach & Strategy

Use a Trie to store all identifiers and dynamic programming to find the maximum number of identifiers that can be used to form the target string. The time complexity is O(N * L^2) where N is the number of identifiers and L is the length of the target string.

Brute Force Approach

Generate all permutations of the input list and check if any permutation's concatenation matches the target string. This approach has a time complexity of O(N! * L) where N is the number of identifiers and L is the length of the target string.

Verified Code Solutions

JavaScript Solution
Time: O(N * L^2)
function solution(arr, k) {
   let count = 0;
   for (let i = 0; i < arr.length; i++) {
       for (let j = 0; j < arr[i].length; j++) {
           if (arr[i][j] > k) {
               count++;
           }
       }
   }
   return count;
}

Asked in Top Tech Interviews

GoogleAmazonMicrosoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.