Bigger is Greater
medium⏱ 15 mintypescriptchallengesproblem-solving-intermediate-
APRENDE HACIENDOPon a prueba tu intuición3 pasos
Bigger is Greater
Usa los ejemplos y la explicación de la lección para comprobar tu idea.
Reflexión personal. No se envía a ningún servicio de IA.ba
no answer
hegf
dhkc
hcdk
Caso de ejemplo — tu programa lee de stdin e imprime en stdout.
ENEste enunciado todavía está solo en inglés; la traducción al español está en camino.
Código inicial Find the lexicographically smallest string greater than w by rearranging its letters (next permutation algorithm). If no such string exists, return 'no answer'.
Algorithm: (1) Find the rightmost position i where w[i-1] < w[i]. (2) Find the rightmost position j where w[j] > w[i-1]. (3) Swap w[i-1] and w[j]. (4) Reverse the suffix from position i to the end.
Example: 'hefg' → find pivot at 'e' (position 1) → swap 'e' with 'g' → 'hgef' → reverse suffix 'ef' → 'hegf'
Formato de entrada
First line: T (number of test cases). Next T lines: each contains a string w.
Formato de salida
For each test case, output the next lexicographically greater string or 'no answer'.
Restricciones
1 ≤ T ≤ 10⁵, 1 ≤ |w| ≤ 100, w contains only lowercase letters