Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
enumerate(text.split()) and increasing indexes solution in Speedy category for Words Order by Phil15
def words_order(text: str, words: list) -> bool:
# A word that appears twice make this simple.
if len(set(words)) != len(words):
return False
# Look for words indexes with a simple iteration on text words.
words = {word: -1 for word in words} # A dict remembers insertion order.
for n, text_word in enumerate(text.split()):
if text_word in words and words[text_word] == -1:
words[text_word] = n
# Make sure all words are in the text and indexes are increasing.
last = -1
for index in words.values():
if index <= last:
return False
last = index
return True
March 6, 2020
Comments: