So I was asked an interesting question recently related to strings and substring. Still trying to get the most optimal answer to this. I'll prefer answer in Java though any psuedo-code/language will be good as well.
The question is:
I am given a string S. I have to divide it into maximum number of substrings(not subsequence) such that no substring has character which is present in another substring.
Examples:
1.
S = "aaaabbbcd"
Substrings = ["aaaa","bbb","c","d"]
2.
S = "ababcccdde"
Substrings = ["abab","ccc","dd","e"]
3.
S = "aaabbcccddda"
Substrings = ["aaabbcccddda"]
Will be really glad if I can get a solution which is better than O(n^2)
Thanks for the help.
It can be done in O(n) time.
The idea behind it is to predict where each substring will end. We know that if we read a char, then the last occurrence of this char must be in the same substring it is (otherwise there would be a repeated char in two distinct substrings).
Let's use
abbacacd
as example. Suppose we know the first and the last occurrences of every char in the string.The O(n) solution is: