Task Scheduler

Given a string A representing tasks CPU need to do it. A consists of uppercase English letters where different letters represent different tasks. Tasks could be done without the original order.
Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.

However, there is a non-negative cooling interval B that means between two same tasks,
there must be at least B intervals that CPU is doing different tasks or just be idle.

Find the least number of intervals the CPU will take to finish all the given tasks.



Input Format

The first argument given is string A.
The second argument given is an integer B.

Output Format

Return the least number of intervals the CPU will take to finish all the given tasks.

Constraints

1 <= |A| <= 10000
0 <= B <= 100 

For Example

Input 1:
    A = "AAABBB"
    B = 2
Output 1:
    8
    Explanation 1:
        CPU can finish the tasks in one of the following ways:
            A -> B -> idle -> A -> B -> idle -> A -> B.
            B -> A -> idle -> B -> A -> idle -> B -> A.

Input 2:
    A = "DBAD"
    B = 5
Output 2:
    7
    Explanation 2:
        D -> B -> A -> idle -> idle -> idle -> D
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Task Scheduler on Interview Code Editor
Sign Up
to access hints and editorial solutions for Task Scheduler

Discussion


Loading...
Click here to start solving coding interview questions