FastPrepLexicographically Smallest School Assembly

Lexicographically Smallest School Assembly

Wells Fargo logoWells Fargo● HardFULLTIMEOA
Learn

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) → String

Examples

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.
  • s contains only lowercase English letters.
  • 0 <= k <= 1000.

More Wells Fargo problems

See Wells Fargo hiring insights
public String arrangeStudents(String s, int k) {
  // write your code here
}
s"ddgdghghh"
k2
expected"dghdghdgh"
Checking account…