← Back to challenges

Recursion: N-Length Letter Groups

PythonHardrecursionsortingstringsarrays

Instructions

Write a function that returns an array of strings populated from the slices of n-length characters of the given word (a slice after another while n-length applies onto the word).

Examples

collect("intercontinentalisationalism", 6)
➞ ["ationa", "interc", "ntalis", "ontine"]

collect("strengths", 3)
➞ ["eng", "str", "ths"]

collect("pneumonoultramicroscopicsilicovolcanoconiosis", 15)
➞ ["croscopicsilico", "pneumonoultrami", "volcanoconiosis"]

Notes

  • Ensure that the resulting array is lexicographically ordered.
  • Return an empty array if the given string is less than n.
  • You are expected to solve this challenge via a recursive approach.
  • An iterative version of this challenge can be found via this link.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.