please suggest efficient solution.
I am assuming that you want to generate distinct substrings of a string.
U could do this using trie in O(N^2)
Just insert all suffix of the string in trie,and then count the number of nodes is trie.To get the strings u can do dfs.
lets say abba
Root / \ a. b / \ b. b / \ b. a /
Ans 7 a,ab,abb,abba,b,bb,bba
Edit:- Sorry i misread the question.If the substring can have only unique characters then u can do simple bruteforce as @ayushkapadia said.
If you want to generate substrings which have only unique characters, then it is very simple done in O (N X 26). As the length of that substring will not be greater than 26 according to pegion hole principle. So just do the brute force that is for every i loop over all j greater than i, but stop whenever you see a repeated character.I am not considering printing time here.But in the worst case it will be O (26 X 26 X N).
for i in range (0,n): myDict = dict () for j in range (i,n): if s [j] in myDict: break myDict[s [j]] = 1 print s [i:j+1]
Oohk I think i misread the question.
Thanks for correcting.