You are given a string query and an array of strings words.
The edit distance (Levenshtein distance) between two strings is the minimum number of single-character operations (insert a character, delete a character, or substitute a character) needed to transform one string into the other.
Return the element of words with the smallest edit distance to query. If several words share the smallest distance, return the lexicographically smallest of them.
Examples
Example 1
Input: query = "horse", words = ["ros", "rose", "house"]
Output: "house"
Explanation: The edit distances to "horse" are: "ros" -> 3, "rose" -> 2,
"house" -> 1 (substitute 'r' with 'u'). "house" is the unique minimum.
Example 2
Input: query = "cat", words = ["cut", "cot", "car"]
Output: "car"
Explanation: All three words are at edit distance 1 from "cat". The
lexicographically smallest of them is "car".
Constraints
1≤words.length≤1000
1≤query.length, words[i].length≤100
query and all words[i] consist of lowercase English letters.
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up