I am trying to find all substring of a string. It works great with smaller length size strings but when length of string is increased such as 5000, it gives memory error. Is there any way to find substring of long string?

```
S="sample string"
all=[]

for i in range(0,len(S)):
    for j in range(0,len(S)):
        if len(S[i:j])>0 and S[i:j] not in all:
            all.append(S[i:j])
        if len(S[-i:])>0 and S[-i:] not in all:
            all.append(S[-i:])

```