Problem · String

Split Prefix Suffix

Learn this problem
MediumAmazonOA
See Amazon hiring insights

Problem statement

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 split the list of items into two contiguous non-empty sub-lists - a prefix and a suffix. To ensure that both days share a sufficient number of similar items, they also need to split it in a way such that the number of distinct categories shared by both sub-lists is greater than k.

Formally, given a string, categories, find the number of ways to split 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.

Function

splitPrefixSuffix(categories: String, k: int) → int

Complete the function splitPrefixSuffix in the editor.

splitPrefixSuffix has the following parameters:

  1. string categories: the categories
  2. int k: shared distinct categories must be greater than this value

Returns

int: the number of ways to split the given string

Examples

Example 1

categories = "abbcac"k = 1return = 2
Example 1 illustration

Constraints

  • 1 ≤ length(categories) ≤ 10^5
  • 0 ≤ k ≤ 26
  • The string categories consists of lowercase English characters.

More Amazon problems

drafts saved locally
public int splitPrefixSuffix(String categories, int k) {
  // write your code here
}
categories"abbcac"
k1
expected2
checking account