You're creating a few lists where you don't need them. I don't know if this fixes the timeout problem, but here's my take:
def countF(s):
l=len(s)
substrings =return sum(s[i:j]int(s[i]==s[j-1]) for i in range(l) for j in range(i+1,l+1))
f = lambda x: int(x[0]==x[-1])
return sum(f(substring) for substring in substrings)
print(countF("ababaca")) #14
First of all, I'm not transforming the string into a list — it's not needed, you can get to a specific characters using square brackets just like you could in a list.
Second, instead of a list comprehension, I'm using a generator comprehension — this way no list is built up in memory. For example, given a very long string, your code needs to build a list the size of the cardinality of possible substrings, which can get quite large. Using my generator, it sums up while looping, so it's always just a single number it needs to keep in memory.
edit: Good point about the slicing, this way should be better.
You're creating a few lists where you don't need them. I don't know if this fixes the timeout problem, but here's my take:
def countF(s):
l=len(s)
substrings = (s[i:j] for i in range(l) for j in range(i+1,l+1))
f = lambda x: int(x[0]==x[-1])
return sum(f(substring) for substring in substrings)
print(countF("ababaca")) #14
First of all, I'm not transforming the string into a list — it's not needed, you can get to a specific characters using square brackets just like you could in a list.
Second, instead of a list comprehension, I'm using a generator comprehension — this way no list is built up in memory. For example, given a very long string, your code needs to build a list the size of the cardinality of possible substrings, which can get quite large. Using my generator, it sums up while looping, so it's always just a single number it needs to keep in memory.
You're creating a few lists where you don't need them. I don't know if this fixes the timeout problem, but here's my take:
def countF(s):
l=len(s)
return sum(int(s[i]==s[j-1]) for i in range(l) for j in range(i+1,l+1))
print(countF("ababaca")) #14
First of all, I'm not transforming the string into a list — it's not needed, you can get to a specific characters using square brackets just like you could in a list.
Second, instead of a list comprehension, I'm using a generator comprehension — this way no list is built up in memory. For example, given a very long string, your code needs to build a list the size of the cardinality of possible substrings, which can get quite large. Using my generator, it sums up while looping, so it's always just a single number it needs to keep in memory.
edit: Good point about the slicing, this way should be better.
You're creating a few lists where you don't need them. I don't know if this fixes the timeout problem, but here's my take:
def countF(s):
l=len(s)
substrings = (s[i:j] for i in range(l) for j in range(i+1,l+1))
f = lambda x: int(x[0]==x[-1])
return sum(f(substring) for substring in substrings)
print(countF("ababaca")) #14
First of all, I'm not transforming the string into a list — it's not needed, you can get to a specific characters using square brackets just like you could in a list.
Second, instead of a list comprehension, I'm using a generator comprehension — this way no list is built up in memory. For example, given a very long string, your code needs to build a list the size of the cardinality of possible substrings, which can get quite large. Using my generator, it sums up while looping, so it's always just a single number it needs to keep in memory.
You're creating a few lists where you don't need them. I don't know if this fixes the timeout problem, but here's my take:
def countF(s):
l=len(s)
substrings = (s[i:j] for i in range(l) for j in range(i+1,l+1))
f = lambda x: int(x[0]==x[-1])
return sum(f(substring) for substring in substrings)
print(countF("ababaca")) #14
You're creating a few lists where you don't need them. I don't know if this fixes the timeout problem, but here's my take:
def countF(s):
l=len(s)
substrings = (s[i:j] for i in range(l) for j in range(i+1,l+1))
f = lambda x: int(x[0]==x[-1])
return sum(f(substring) for substring in substrings)
print(countF("ababaca")) #14
First of all, I'm not transforming the string into a list — it's not needed, you can get to a specific characters using square brackets just like you could in a list.
Second, instead of a list comprehension, I'm using a generator comprehension — this way no list is built up in memory. For example, given a very long string, your code needs to build a list the size of the cardinality of possible substrings, which can get quite large. Using my generator, it sums up while looping, so it's always just a single number it needs to keep in memory.
You're creating a few lists where you don't need them. I don't know if this fixes the timeout problem, but here's my take:
def countF(s):
l=len(s)
substrings = (s[i:j] for i in range(l) for j in range(i+1,l+1))
f = lambda x: int(x[0]==x[-1])
return sum(f(substring) for substring in substrings)
print(countF("ababaca")) #14