ok[i] tells whether
s[:i] can be built.
def wordBreak(self, s, words): ok = [True] for i in range(1, len(s)+1): ok += any(ok[j] and s[j:i] in words for j in range(i)), return ok[-1]
gotcha, so the comma works as brackets in this situation, right? How about other cases, I mean, you can put it at any end of a line.
No, not brackets. Brackets create a list. The comma creates a tuple. Don't know what you mean with "can put it at any end of a line" but it sounds wrong (as it can change the meaning of the line).
It seems quite a lot faster than even
append, btw, which is one reason I like it:
>>> from timeit import timeit >>> timeit('x.append(1)', 'x = ', number=10000000) 1.9880003412529277 >>> timeit('x += 1,', 'x = ', number=10000000) 1.2676891852971721 >>> timeit('x += ', 'x = ', number=10000000) 3.361207239950204
@StefanPochmann if I understand it right here, initiate x = 
then x += 1 gives same result as x.append(1), but much faster.
But when I tried x += 1, it gives an error " 'int' object is not iterable" for both Python2.x and Python 3. Could you correct me if I miss something?
@Red_Eden you missed the comma at the end.
@jie19 Thank you so much. It's a very interesting way to append to a list
@StefanPochmann I am not sure about the time complexity here, is it O(n^2) ?
Make it faster :)
def wordBreak(self, s, words): ok, words_set, word_max_len = [True], set(words), len(max(words, key=len)) if words else 0 for i in xrange(1, len(s) + 1): ok += any(ok[j] and s[j:i] in words_set for j in xrange(max(0, i - word_max_len), i)), return ok[-1]
@Acton shi zhen de diao
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.