Split the String into Substrings with Non-overlapping Characters

Published by Evgeny SH in

The function is given a string with lower-case characters. Split the string into as many substrings as possible such that each character appears in only one substring. Return the list of lengths of the resulting substrings.

Examples

split_string("abbccc"), [1, 2, 3]
# "a", "bb", "ccc"

split_string("abbacdceef"), [4, 3, 2, 1]
# "abba", "cdc", "ee", "f"

split_string("abacded"), [3, 1, 3]
# "aba", "c", "ded"

split_string("abcdea"), [6]
# "abcdea" because first letter is equal to the last letter.

Notes

Xavier would deeply appreciate if you solve this challenge with recursion using greedy approach.

Watch a quick demo on how Edabit works.