#crosscompute
"""
Given a string S and a set of words D, find the longest word in D that is a subsequence of S.
Word W is a subsequence of S if some number of characters, possibly zero, can be deleted from S to form W, without reordering the remaining characters.
Note: D can appear in any format (list, hash table, prefix tree, etc.
For example, given the input of S = "abppplee" and D = {"able", "ale", "apple", "bale", "kangaroo"} the correct output would be "apple"
The words "able" and "ale" are both subsequences of S, but they are shorter than "apple".
The word "bale" is not a subsequence of S because even though S has all the right letters, they are not in the right order.
The word "kangaroo" is the longest word in D, but it isn't a subsequence of S.
"""
S = "abppplee"
D = ["able", "ale", "apple", "bale", "kangaroo"]
w = list(D[4])
print(w)
for letter in w:
if letter not in S:
print(letter, 'not in', S)
else:
print(letter, 'in', S)
# eliminate words with invalid letters
# eliminate words with letters in wrong order
for i in w:
if i not in S:
print(i, 'not in', S)
# step 1: take each i in w
# check each j in S
# when i == j:
# slice off preceding letters in S
# if len(S) < len(w) - 1, eleminate w
# else, slice off i from w
# if len(w) == 0: append word to result
# if len(w) > 0 and len(S) >= len(w): repeat step 1
# else: go to next word
w = list(D[0])
def yield_match(w, S):
for i in w:
for j in S:
if i == j:
w.pop(w.index(i))
S = S[S.index(j)+1:]
yield i,j
m = yield_match(w, S)
next(yield_match)
w.index('a')
w[w.index('b') + 1:]