Ctrl K

A Forgiving Dictionary

Medium
You have a dictionary and a list of queries. For each query find its correct spelling using these rules, tried in order (the first one that applies wins): 1. If the query appears in the dictionary exactly (case-sensitive), return it as is. 2. Otherwise, if some dictionary word equals the query ignoring case, return the first such word as written in the dictionary. 3. Otherwise, if some dictionary word equals the query ignoring case once any vowel may stand in for any other vowel (one for one, the length stays the same), return the first such word from the dictionary. 4. Otherwise, return an empty string. The vowels are a, e, i, o, u. Return the answers in the order of the queries.
Example 1
dictionary=['Mango', 'lemon', 'melon'], queries=['mango', 'MANGO', 'lemon', 'limon', 'milan', 'lime'] → ['Mango', 'Mango', 'lemon', 'lemon', 'melon', '']

mango and MANGO match Mango ignoring case. lemon is in the dictionary verbatim. limon differs from lemon by one vowel. milan fits melon. Nothing fits lime.

Example 2
dictionary=['tea', 'Tea', 'toe'], queries=['TEA', 'tia', 'Tea'] → ['tea', 'tea', 'Tea']

For TEA the first case-insensitive match is tea. For tia the first vowel-tolerant match is also tea. Tea is in the dictionary exactly, so it comes back unchanged.

Constraints
  • 1 ≤ len(dictionary), len(queries) ≤ 5000
  • 1 ≤ length of each word ≤ 50
  • Words consist of English letters only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAnagram Buckets MediumSplit Into Dictionary Words MediumWords Equal Up to a Shift MediumA Subsequence of One Word Only MediumFiles With the Same Content Medium
loading Python…
Input
dictionary=['Mango', 'lemon', 'melon'], queries=['mango', 'MANGO', 'lemon', 'limon', 'milan', 'lime']
Expected
['Mango', 'Mango', 'lemon', 'lemon', 'melon', '']

Run checks the first 3 tests in your browser. Submit checks all 88 tests on the server, hidden ones included.