Advertisement
AlaminSakib

anagram hashtable solution

Oct 24th, 2022
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. from collections import Counter
  2. from typing import List
  3.  
  4.  
  5. def find(words: List[str], needle: str) -> str:
  6.     needle_frequency = Counter(needle)
  7.  
  8.     for word in words:
  9.         if len(word) <= len(needle):
  10.             word_frequency = Counter(word)
  11.             matched_word = word
  12.             for key in word_frequency.keys():
  13.                 if key not in needle_frequency or needle_frequency[key] < word_frequency[key]:
  14.                     matched_word = ""
  15.                     break
  16.             if len(matched_word):
  17.                 return matched_word
  18.     return "-"
  19.  
  20.  
  21. print(find(["baby", "referee", "cat", "dada", "dog", "bird", "ax"], "ctay"))
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement