0

I have to form several sub-strings from a single string.

Usual nested for loops with i and j is giving correct result but having time out issue for larger string values(>3K length).

Looking for comprehension or any other solution that might give quick results.

for i in range((len(string)+1)):
    for j in range(i+1, (len(string)+1)):
        substring_list.append(string[i:j])
k92
  • 375
  • 3
  • 15
  • does `substring_list = [string[i:j] for i in range(len(string)+1) for j in range(i+1, len(string)+1)]` go any faster? I wouldn't expect it to, since it's doing roughly the same amount of work, but it's a list comprehension of what you have there. I don't think you'll get overwhelmingly much of a performance improvement on this without really fancy `itertools` stuff, just because this many permutations on such a large string is a lot of work – Green Cloak Guy Dec 13 '19 at 03:35
  • 1
    Does this answer your question? [How To Get All The Contiguous Substrings Of A String In Python?](https://stackoverflow.com/questions/22469997/how-to-get-all-the-contiguous-substrings-of-a-string-in-python) – Selcuk Dec 13 '19 at 03:44
  • if you are running on local just increase time limit – Adam Strauss Dec 13 '19 at 05:55

0 Answers0