FastPrepWelsh Custom Alphabet Sort

Welsh Custom Alphabet Sort

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Sort words using this Welsh alphabet of tokens:

a, b, c, ch, d, dd, e, f, ff, g, ng, h, i, l, ll, m, n, o, p, ph, r, rh, s, t, th, u, w, y

The tokens ch, dd, ff, ng, ll, ph, rh, and th are single alphabet symbols. Tokenize each word greedily using these two-letter symbols before single letters, then compare token ranks from left to right. If one token sequence is a prefix of another, the shorter word comes first.

Function

sortWelshWords(words: String[]) → String[]

Examples

Example 1

words = ["ddr","nah","dea","dd","ngah"]return = ["dea","dd","ddr","ngah","nah"]

d precedes dd; dd is a prefix of ddr; and ng precedes n in the supplied token alphabet.

Example 2

words = ["ca","cha","da","dda"]return = ["ca","cha","da","dda"]

The leading tokens follow c < ch < d < dd.

Constraints

  • 1 <= words.length <= 10^5.
  • Every word is nonempty, lowercase, and can be tokenized by the alphabet above.
  • The total number of characters is at most 2 * 10^5.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[] sortWelshWords(String[] words) {
  // Write your code here.
}
words["ddr","nah","dea","dd","ngah"]
expected["dea", "dd", "ddr", "ngah", "nah"]
Checking account…