Perfect scorers are exactly the students whose strings equal the key. For , some string must have frequency . For , the number of distinct strings must be less than . If , then , so answer yes without evaluating the power. Sorting gives worst-case time and space. The shift is evaluated only for , so it is at most . The small-subtask naive solution enumerates all keys in time.
Solution written by GPT6