Skip to main content
1 of 4
coltim
  • 6.2k
  • 8
  • 13

K (ngn/k), 19 bytes

1+?{*(++,\x)?,x}',\ 

Try it online!

Modeled after @Jonah's J answer.

  • {...}',\ for each prefix of the (implicit) input...
  • (++,\x) expand each prefix (of the current passed-in prefix) to the full length of that prefix (e.g., transform 1 1 2 from (1;1 1;1 1 2) into (1 1 1;1 1 1;1 1 2))
  • *(...)?,x get the index of the first transformed prefix that matches the prefix itself
  • 1+?{...} add one to the distinct indices
coltim
  • 6.2k
  • 8
  • 13