Oscar Health member search uses a specialized ordering for labels displayed in search results. Given a list of lowercase words and a custom alphabet, sort the words lexicographically, but rotate the alphabet by one position for every character depth.
At depth d, characters are ordered as alphabet[d:] + alphabet[:d], where d is zero-based. A word that is a prefix of another word comes first. Duplicate words must remain duplicated, and their input order must be preserved.
Return the sorted list. Design the algorithm to avoid comparing every pair of words directly.
Implement sort_rotated_words(words, alphabet).
words is a list of strings containing only characters from alphabet.alphabet is a permutation of between 2 and 26 distinct lowercase letters.def sort_rotated_words(words, alphabet):