I have a simple string "abc". Now I have to write Python code for creating all possible unique substrings, except the empty string, from this string.
The answer should be as follows: a b c ab ac bc abc
I wrote something like this :
def getsubstrings(str):
sub = []
length = len(str)
for i in range(length):
for j in range(i,length):
sub.append(str[i:j+1])
sub.sort()
return sub
str = "abc"
print (getsubstrings(str))
But this is probably the wrong way , since it is not giving expected results.
Can someone please help me with an efficient solution.
Thanks in advance.