Lexicographically Smallest School Assembly
Problem statement
You are given a lowercase string s and a nonnegative integer k. Rearrange all characters of s so that between any two equal characters there are at least k other characters.
If no valid rearrangement exists, return "-1". Otherwise, return the lexicographically smallest valid rearrangement.
Function
arrangeStudents(s: String, k: int) → StringExamples
Example 1
s = "ddgdghghh"k = 2return = "dghdghdgh"Each repeated class has exactly two other students between consecutive occurrences, and this is the lexicographically smallest valid ordering.
Example 2
s = "aabbcc"k = 2return = "abcabc"Each letter repeats after two intervening characters.
Example 3
s = "aaabc"k = 1return = "abaca"The three a characters are separated by one character each; choosing b before c gives the smallest valid result.
Example 4
s = "aabb"k = 3return = "-1"The string is too short to put three other characters between both copies of each repeated letter.
Example 5
s = "cba"k = 0return = "abc"With no required gap, sorting the characters gives the lexicographically smallest arrangement.
Constraints
1 <= s.length <= 10^5.scontains only lowercase English letters.0 <= k <= 1000.