Hello,
I submitted a solution for the Implement Magic Dictionary problem and there is one testcase that failed (#69 - The input is too large to paste in here) .
When I copy the failed test case from the input, and submit that to Run Code, the same failing test passes. I'm having a difficult time understanding why, when it runs through all 83 of the test cases I get a different response from #69 than when it runs through that test by itself.
The error message does not say there is a time limit exceeded error either.
My code is a brute force solution but I would like to better understand what I'm missing. Thank you!
class MagicDictionary:
def __init__(self, current = {}):
"""
Magic dictionary will create a dictionary with distinct keys and will check input to see if we
can change one letter or our current keys to match the given word.
"""
self.current = current
def buildDict(self, keys: List[str]) -> None:
"""Add in the keys to the current."""
for key in keys:
word_length = len(key)
words = self.current.get(word_length, set())
words.add(key)
self.current[word_length] = words
def search(self, word: str) -> bool:
"""
Search through the dictionary to see if we are able to change one letter, of any of the keys
to create the given word.
"""
potential_words = self.current.get(len(word))
if not potential_words:
return False
return any([self.compare_words(word, potential_word) for potential_word in potential_words])
@staticmethod
def compare_words(word: str, given_word: str):
counter = 0
i = 0
while counter <= 1 and i < len(word):
if word[i] == given_word[i]:
i += 1
else:
counter += 1
if counter > 1:
return False
i += 1
if counter < 1:
return False
return True