I'm currently trying to figure out whether a list of strings is a valid sublist of another list of strings. This question has been asked many times before but I haven't seen a solution for when either the list or sublist includes duplicates.
Say the list of letters is ['A', 'B', 'C', 'D'] and the word is ['A', 'B', 'C']. There are many ways to check that the word is a valid sublist. But if the word is ['A', 'B', 'C', 'C'], I need the output to return that the word is an invalid sublist, because there aren't two available "C's" in the list of letters. This means that using the all() function as well as sets doesn't work because they do not check for duplicates properly.
I have also tried using some sort of tracker and string.rfind() to see if all of the letters in the sublist are letters in the list, but this fails as well. Here is the code that I tried:
def firstFilter(dictionary, board):
initFilter = []
for word in dictionary:
tracker = 0
if 3 <= word <= 16:
for j in range(len(board)):
for k in range(len(board[j])):
letter = board[j][k]
check = word.rfind(letter)
if check != -1:
tracker += 1
if tracker >= len(word):
initFilter.append(word)
initFilter = sorted(initFilter, key=len, reverse=True)
print('Words available:', len(initFilter))
return initFilter
This code checks each letter in a list of letters to see if the letter is found in the word, where the full list of letters is the list, and each word is the sublist. But this method also has an issue. If there are duplicates in the full letter list, then the tracker is longer than the length of the word. For instance, if the letter list is ['A', 'B', 'C', 'C'] and word is ['A', 'B', 'C'], then the tracker takes value 4 and the length of the word is 3. This is why I use >= and not ==.
But this runs into another problem if the letter list is ['A', 'B', 'C', 'C'] and the word list is and word is ['A', 'B', 'C', 'D']. The duplicate letters in the letter list cause the tracker to take value 4, and the word length is 4, so the code returns that the word is a valid sublist of the letter list, even though there is no 'D' in the letter list.
Is there any way I can avoid both of these problems? I am a novice Python programmer, but I couldn't find any help from past questions on this site because none of them I could find addressed both of these duplicate issues when the list and/or the sublist contains duplicates.