UBER Coding Question – Solved

2 Live
Q Trip Tag Generator Uber's platform auto-generates trip tags which are short strings summarizing key trip metadata. To validate the tagging logic, the infrastructure team is checking how many different ways a specific tag can be constructed using segments from historical tags. You are given an array tags of length n, where each tag is a string of length m. You're also given a targetTag that needs to be formed by picking characters from the tags as follows: Characters in targetTag must be picked in order (left to right). For the i-th character in targetTag, pick it from any tag but only from position j such that indices j are strictly increasing as you build the tag. You may pick multiple characters from the same tag. Two ways are considered different if the positions used or tags selected differ at any step. Implement a function that determines the number of distinct ways to form the targetTag using the given rules. The function getTotalDistinctWays takes the following inputs: string tags[n]: List of strings, each of equal length; string targetTag: the string that needs to be constructed. The function should return an integer denoting the number of ways to form the targetTag, modulo 10^9 + 7. Example: n = 3 tags = ["adc", "aec", "efg"] targetTag = "ac" There are 4 distinct ways to reach the targetTag: 1. Select the 1st character of "adc" and the 3rd character of "adc". 2. Select the 1st character of "adc" and the 3rd character of "aec". 3. Select the 1st character of "aec" and the 3rd character of "adc". 4. Select the 1st character of "aec" and the 3rd character of "aec". Constraints: - 1 ≤ n ≤ 10^3 - 1 ≤ length of tags[i] ≤ 3000 - All tags[i] are of equal length per test case. - The sum of the lengths of all tags[i] ≤ 10^5 - 1 ≤ length of targetTag ≤ length of tags[0] - All characters are lowercase English letters. Input Format For Custom Testing Sample Case 0 Sample Input For Custom Testing STDIN valya lygtb vldoh val Sample Output 4 Explanation: There are 4 ways to construct the targetTag "val" such that the indices will be in strictly increasing order. - Select the 1st character of "valya", the 2nd character of "valya" and the 3rd character of "valya". - Select the 1st character of "valya", the 2nd character of "valya" and the 4th character of "lygtb". - Select the 1st character of "vldoh", the 2nd character of "valya" and the 3rd character of "valya". - Select the 1st character of "vldoh", the 2nd character of "valya" and the 4th character of "lygtb". Sample Case 1 Function tags[] size n = 3 tags = ["valya", "lyglb", "vldoh"] targetTag = "val"

Asked in: UBER

Image of the Question

Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


from sys import setrecursionlimit
setrecursionlimit(10**5)
from functools import lru_cache
def getTotalDistinctWays(tags, targetTag):
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem, you need to think about how to count the number of distinct ways to form the targetTag from multiple tags, following specific rules about ordering and position selection.

---

**Step 1: Understand the Problem and Constraints**

You have:
- An array of n tags, each of the same length m.
- A targetTag string you want to form by choosing characters from these tags.

The key constraints are:
- Characters in targetTag must be chosen in order (from left to right).
- For the i-th character of targetTag, you pick it from any tag at position j (0-based indexing in implementation), where the positions j are strictly increasing as you move through the targetTag.
- You can pick multiple characters from the same tag.
- Two ways differ if the positions or the tags chosen at any step differ.

This means that if you look at the positions in the tags used to form the targetTag, these positions must form a strictly increasing sequence, regardless of which tag each character is picked from.

---

**Step 2: Naive Idea and Its Challenges**

A brute-force method would be to try all possible sequences of indices and tags for each character in the targetTag, checking whether the characters match and the indices are strictly increasing. However, this approach is clearly infeasible because the number of possible combinations is huge (exponential in length of targetTag and tags).

---

**Step 3: Key Insight - Fixing Position and Character Choices**

Since the order of positions strictly increases for characters in targetTag, the positions at which you pick characters correspond to a strictly increasing sequence of indices from 0 to m-1 (the length of each tag).

For each position j (from 0 to m-1), you can look at all characters in all tags at that position. You can precompute how many tags have a particular character at each position j.

This lets you understand, for each position in the tags, how many ways you can pick a character to match a given character of the targetTag.

---

**Step 4: Dynamic Programming Formulation**

You want to count the number of ways to form the first i characters of targetTag by choosing positions in strictly increasing order.

Define a DP state:
- Let dp[i][j] represent the number of ways to form the first i characters of targetTag, ending at position j (i.e., the i-th character of targetTag is chosen from position j in the tags).

The base case:
- For i = 0 (first character), dp[0][j] is the number of tags that have the first character of targetTag at position j.

Transition:
- To compute dp[i][j] (ways to pick i-th character of targetTag at position j), sum over all dp[i-1][k] for all positions k < j, and multiply by the number of tags that have targetTag[i] at position j.

Because you must pick characters in increasing order of positions, only consider k < j.

---

**Step 5: Efficient Calculation**

Naively, computing dp[i][j] by summing over all k < j for each j is O(m^2) per character of targetTag, which might be too large for constraints.

To optimize, use prefix sums for dp[i-1], so that sum over dp[i-1][k] for k < j can be done in O(1) time per j.

For each character in targetTag and for each position j:
- Calculate count of tags with targetTag[i] at position j.
- Use prefix sums to get sum of dp[i-1][k] for k < j.
- Then dp[i][j] = (count * prefixSum of dp[i-1]) modulo 10^9+7.

---

**Step 6: Preprocessing Character Counts**

Precompute a frequency table: for each position j and for each character 'a' to 'z', count how many tags have that character at position j.

This lets you quickly get the "count" values needed in DP transitions.

---

**Step 7: Final Answer**

After filling dp for all characters of targetTag, sum dp[last_char][j] over all positions j to get the total number of distinct ways to form targetTag.

---

**Step 8: Modulo Arithmetic**

Since the number of ways can be large, all arithmetic operations should be done modulo 10^9 + 7.

---

**Step 9: Summary**

- Precompute character counts at each position across all tags.
- Use dynamic programming to count ways to build the targetTag one character at a time, using strictly increasing positions.
- Optimize transitions using prefix sums to avoid O(m^2) complexity.
- Return the sum of ways for the last character at all positions modulo 10^9 + 7.

This approach cleverly reduces a complex combinatorial problem to manageable DP with preprocessing, allowing efficient calculation of the number of ways to construct the targetTag under given constraints.
```


Related Questions