exception Solved of string

module CharMap = Map.Make (Char)

let alphabet =
  [ 'a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'i'; 'j'; 'k'; 'l'; 'm';
    'n'; 'o'; 'p'; 'q'; 'r'; 's'; 't'; 'u'; 'v'; 'w'; 'x'; 'y'; 'z']

let rec solve trie candidates =
  solve trie @@ List.concat @@ List.map (fun (s, i) ->
    List.map (fun c ->
      try (s ^ String.make 1 c, CharMap.find c (trie i))
      with Not_found -> raise (Solved (s ^ String.make 1 c))) alphabet) candidates

let () =
  let a = read_line () in
  let trie = Array.make (String.length a + 1) CharMap.empty in
  for i = String.length a - 1 downto 0 do
    trie.(i) <- CharMap.add a.[i] (i + 1) trie.(i + 1)
  done;
  print_endline @@
  try solve (Array.get trie) [ ("", 0) ]
  with Solved s -> s
