Input
The first line of input contains a string s: the word misspelled by your partner. Assume that your partner did not add or remove any letters; they only replaced letters with incorrect ones. The next line contains a single positive integer n indicating the number of possible valid words that your partner could have meant to write. Each of the next n lines contains each of these words. There is guaranteed to be at least one word of the same length as the misspelled word.Output
Output a single word w: the word in the dictionary of possible words closest to the misspelled word. "Closeness" is defined as the minimum number of different characters. If there is a tie, choose the word that comes first in the given dictionary of words.
Example:
input
deat
6
fate
feet
beat
meat
deer
dean
output
beat
s = input()
n = int(input())
count_list = []
word_list = []
final = []
count = 0
for i in range(n):
N = input()
for j in range(len(N)):
if s[j] == N[j]:
count += 1
count_list.append(count)
word_list.append(N)
count = 0
max_c = count_list[0]
for i in range(len(count_list)):
if count_list[i] > max_c:
max_c = count_list[i]
indices = [index for index, val in enumerate(count_list) if val == max_c]
for i in range(len(indices)):
final.append(word_list[indices[i]])
print(final[0])
This is what I managed to write. This code passed 3 test cases but failed the 4th. I tried to do final.sort() to put them in alphabetical order, but it fails on the 2nd test case (I can only see the 1st test case which is included in this post). What can I do to fix this? The code only allows words with the same number of characters as the control string, which might be causing the wrong test case since one of the test cases might have more or less characters than the control string? Can someone help?