我具有以下代码库,用于检查单词列表(函数的第二个参数)中特定单词(函数的第一个参数)的字谜:
def anagram_checker(word, words=None):
anagrams = []
if not words: # if the second arg list is empty then there is nothing to check
return anagrams
else:
counter_word = Counter(word)
for given_word in words:
if given_word != word: # Cannot be same as the word in question
if Counter(given_word) == counter_word:
anagrams.append(given_word)
return anagrams
因此,如果让我找到函数的时间复杂度,我将像这样:
1. Creating a counter dict for my first argument : O(m) , where m is the length of my first word.
2. Looping through the list of words given and creating counters for each of them is O(n*k) , where n is the number of words in my list and let K be the average length of each word.
3. Comparing two counter objects is O(m).
So if someone asks me to tell them the overall time complexity for me it would O(2m + n*k)
那我怎么说呢?是O(n)还是O(n ^ 2)?
也请告诉我在某处计算复杂度时是否出错。
我在计算复杂度方面非常虚弱,因此非常欢迎任何详细的解释。
谢谢。