428. Find Repeated Candy Batches
Asked in
Find Repeated Candy Batches

Candies are stored in a list in their packing order. Each value represents the flavor of one candy.

Find every consecutive batch containing at least three candies of the same flavor.

Class

CandyBatchAnalyzer

Method

findRepeatedBatches

List<String> findRepeatedBatches( List<String> candyFlavors )

Parameters

  • candyFlavors: The flavors of the candies in their packing order.

Returns

Return one string for each repeated batch in the format "startIndex,endIndex".

Both indices are inclusive. Return the batches in increasing order of their starting indices.

Return an empty list if there are no repeated batches.

Batch Rules

  • Candy positions are indexed from 0.
  • Every candy in a batch must have the same flavor and appear consecutively.
  • A repeated batch must contain at least three candies.
  • A returned range must include the complete uninterrupted batch of that flavor.
  • Candies with the same flavor in separate parts of the list belong to different batches.

Constraints

  • 1 ≤ candyFlavors.size() ≤ 1,000
  • 1 ≤ candyFlavors.get(i).length() ≤ 20
  • Every flavor contains only lowercase English letters.
  • No value in candyFlavors is null.

Example 1

findRepeatedBatches( candyFlavors = List.of( "cherry", "mint", "mint", "mint", "mint", "lemon", "berry" ) )

Output: List.of("1,4")

The four mint candies from indices 1 through 4 form a repeated batch.

Example 2

findRepeatedBatches( candyFlavors = List.of( "apple", "apple", "mint", "mint", "berry" ) )

Output: List.of()

No flavor appears at least three times consecutively.

Example 3

findRepeatedBatches( candyFlavors = List.of( "orange", "orange", "orange", "lime", "berry", "berry", "berry", "vanilla", "vanilla", "vanilla", "vanilla", "mint" ) )

Output: List.of("0,2", "4,6", "7,10")

The orange, berry, and vanilla candies form three separate repeated batches.

Example 4

findRepeatedBatches( candyFlavors = List.of( "mango", "mango", "mango", "mango", "mango" ) )

Output: List.of("0,4")

All five candies form one complete repeated batch.



Please use Laptop/Desktop or any other large screen to add/edit code.