Get Max Alternating Music
Learn this problemProblem statement
Amazon Music is working on harmonizing their music playlist.
In their playlist system, each song is represented by a binary string music, where '0' and '1' denote two different types of music, say TypeA and TypeB. An "alternating music string" is one where no two adjacent songs are of the same type. For example, "1", "0", "10", "01", "101" are alternating music strings.
Given a binary string music representing the playlist and an integer k, you can perform up to k operations where each operation involves flipping a character, i.e., turning '0' into '1' and '1' into '0'.
As a developer at Amazon, the task is to determine the longest alternating substring that can be created from the music string by performing at most k operations.
Note:
- A binary string is a sequence of '0' and '1' characters.
- A string A is a substring of a string B if A can be obtained from B by deleting several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
Function
getMaxAlternatingMusic(music: String, k: int) → int
Complete the function getMaxAlternatingMusic in the editor.
getMaxAlternatingMusic has the following parameters:
string music: a string of characters 0 and 1.int k: the number of operations allowed.
Returns
int: the length of the longest alternating music string that can be created.
Examples
Example 1
music = "1001"k = 1return = 3By flipping the third character, the string becomes music = "1011". The longest alternating music string in this modified string is "101", which spans from the 0th index to the 2nd index and has a length of 3. With only one operation, it is not possible to obtain a longer alternating binary substring. Thus, the answer is 3.
Constraints
🐼More Amazon problems
- Secure Maximum DeliveriesOA · Seen Jul 2026
- Find Median from Data StreamONSITE INTERVIEW · Seen Jul 2026
- Handwritten SigmoidPHONE SCREEN · Seen Jul 2026
- Handwritten SoftmaxPHONE SCREEN · Seen Jul 2026
- Koko Eating BananasONSITE INTERVIEW · Seen Jul 2026
- Loyal Customers Across Two DaysONSITE INTERVIEW · Seen Jul 2026
- Maximum System Memory CapacityOA · Seen Jul 2026
- Package Delivery SystemOA · Seen Jul 2026