The task is to obtain a unique list of substrings in python.
I am currently using the breakup of the problem into 2 parts: obtain a list of all the substrings, followed by obtaining unique substrings.
I am using the below code:
substrings=[]
for i in range(0,len(inputstring)+1):
for j in range(i+1,len(inputstring)+1):
substr=inputstring[i:j]
substrings.append(substr)
uniq=[]
for ss in substrings:
if ss not in uniq:
uniq.append(ss)
Is there a faster way to solve this problem or a so-called python way of doing it in a more flexible way?
a simple example string being: "aabaa"
, possible substrings are [a,a,b,a,a,aa,ab,ba,aa,aab,aba,baa,aaba,abaa,aabaa]
, unique substring which is desired at the end [a,b,aa,ab,ba,aab,aba,baa,aaba,abaa,aabaa]