题目

Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word.

For a given query word, the spell checker handles two categories of spelling mistakes:

  • Capitalization: If the query matches a word in the wordlist (
    case-insensitive
    ), then the query word is returned with the same case as the case in the wordlist.
    • Example: wordlist = ["yellow"], query = "YellOw": correct = "yellow"
    • Example: wordlist = ["Yellow"], query = "yellow": correct = "Yellow"
    • Example: wordlist = ["yellow"], query = "yellow": correct = "yellow"
  • Vowel Errors: If after replacing the vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) of the query word with any vowel individually, it matches a word in the wordlist (
    case-insensitive
    ), then the query word is returned with the same case as the match in the wordlist.
    • Example: wordlist = ["YellOw"], query = "yollow": correct = "YellOw"
    • Example: wordlist = ["YellOw"], query = "yeellow": correct = "" (no match)
    • Example: wordlist = ["YellOw"], query = "yllw": correct = "" (no match)

In addition, the spell checker operates under the following precedence rules:

  • When the query exactly matches a word in the wordlist (case-sensitive), you should return the same word back.
  • When the query matches a word up to capitlization, you should return the first such match in the wordlist.
  • When the query matches a word up to vowel errors, you should return the first such match in the wordlist.
  • If the query has no matches in the wordlist, you should return the empty string.

Given some queries, return a list of words answer, where answer[i] is the correct word for query = queries[i].

Example 1:

  1. Input: wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
  2. Output: ["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]

Note:

  • 1 <= wordlist.length <= 5000
  • 1 <= queries.length <= 5000
  • 1 <= wordlist[i].length <= 7
  • 1 <= queries[i].length <= 7
  • All strings in wordlist and queries consist only of english letters.

题目

给定一个单词字典list和一个查询列表,针对查询列表中的每一个单词word判断:

  1. 如果大小写敏感,word是否在list中出现
  2. 如果大小写不敏感,word是否在list中出现,返回第一个出现的单词
  3. 如果大小写不敏感,且word中的元音字母可以任意替换其他的元音字母,转换后的word是否在list中出现,返回第一个出现的单词
  4. 都不满足,返回空字符串

思路

对wordlist中的单词word进行预处理,维护三个HashMap:(word -> index)、(word.toLowerCase() -> index)、(word.toLowerCase().将所有元音字母转换为’#’() -> index)。预处理后,对每一个query按顺序处理并在三个HashMap中查找即可。


代码实现

Java

  1. class Solution {
  2. public String[] spellchecker(String[] wordlist, String[] queries) {
  3. String[] ans = new String[queries.length];
  4. Map<String, Integer> origin = new HashMap<>();
  5. Map<String, Integer> lower = new HashMap<>();
  6. Map<String, Integer> trans = new HashMap<>();
  7. for (int i = 0; i < wordlist.length; i++) {
  8. String word = wordlist[i];
  9. origin.putIfAbsent(word, i);
  10. lower.putIfAbsent(word.toLowerCase(), i);
  11. trans.putIfAbsent(transform(word), i);
  12. }
  13. for (int i = 0; i < queries.length; i++) {
  14. String query1 = queries[i];
  15. if (origin.containsKey(query1)) {
  16. ans[i] = wordlist[origin.get(query1)];
  17. continue;
  18. }
  19. String query2 = query1.toLowerCase();
  20. if (lower.containsKey(query2)) {
  21. ans[i] = wordlist[lower.get(query2)];
  22. continue;
  23. }
  24. String query3 = transform(query1);
  25. if (trans.containsKey(query3)) {
  26. ans[i] = wordlist[trans.get(query3)];
  27. continue;
  28. }
  29. ans[i] = "";
  30. }
  31. return ans;
  32. }
  33. private String transform(String s) {
  34. s = s.toLowerCase();
  35. StringBuilder sb = new StringBuilder();
  36. for (char c : s.toCharArray()) {
  37. if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
  38. sb.append('#');
  39. } else {
  40. sb.append(c);
  41. }
  42. }
  43. return sb.toString();
  44. }
  45. }