AMAZON Coding Question – Solved

10 Live
Amazon Prime Day is a day where many items are put on sale for Amazon Prime members. A list of sale items is assembled where each item is assigned a category denoted by a lowercase English letter. Since the sale is to be held on two different days, the company has decided to partition the list of items into two contiguous non-empty sub-lists - a prefix and a suffix. To ensure that both the days share a sufficient number of similar items, they also need to partition it in a way such that the number of distinct categories shared by both the sub-lists is greater than k. Formally, given a string, itemCategories, find the number of ways to partition the string into exactly two contiguous non-empty substrings such that the number of distinct characters occurring in both the substrings is greater than a given integer k. Example: Consider itemCategories = "abbcac" and k = 1. We can partition the list of categories into exactly two sub-lists (substrings) in the following ways: There are two ways to partition the list of categories such that the number of distinct characters shared by both the substrings is greater than 1, so return 2. Function Description: Complete the function countValidPartitions in the string itemCategories: the categories; int k: shared distinct categories must be greater than this value. Returns: int: the number of ways to partition the given string. Constraints: · 1 ≤ length(itemCategories) ≤ 10^5 · 0 ≤ k ≤ 26 · The string itemCategories consists of lowercase English characters. Input Format For Custom Testing: Sample Case 0 Sample Input For Custom Testing: STDIN 4 adbccdbada 2 Sample Output: 4 Explanation: The number of ways to partition is 4. Assuming 0-based indexing, we can partition at -

Asked in: AMAZON

Image of the Question

Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


def countValidPartitions(itemCategories, k):
    prefix = {}
    suffix = {}
    for i in itemCategories:
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


No explanation available for this question.


Related Questions