FastPrepMinimum Length After Spacing Equal Characters

Minimum Length After Spacing Equal Characters

Amazon logoAmazon● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

Given a non-empty string of lowercase English letters and a nonnegative integer k, rearrange all characters and insert any number of placeholder positions so that every two equal characters have at least k positions between them.

Return the minimum possible total sequence length, including placeholders. The output asks only for the length, not one arrangement.

Function

minimumRearrangedLength(characters: String, k: int) → long

Examples

Example 1

characters = "abaaab"k = 3return = 13

Four copies of a require three groups of at least three separating positions.

Example 2

characters = "abaacd"k = 4return = 11

Three copies of a force a span of 11 positions.

Constraints

  • 1 <= characters.length <= 200000
  • characters contains only lowercase English letters.
  • 0 <= k <= 10^9
  • The answer fits in a signed 64-bit integer.

Source note: Interview prompt excerpt preserved as source evidence.

More Amazon problems

See Amazon hiring insights
public long minimumRearrangedLength(String characters, int k) {
    // Write your code here.
}
characters"abaaab"
k3
expected13
Checking account…