zoukankan      html  css  js  c++  java
  • 0966. Vowel Spellchecker (M)

    Vowel Spellchecker (M)

    题目

    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:

    Input: wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
    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

    class Solution {
        public String[] spellchecker(String[] wordlist, String[] queries) {
            String[] ans = new String[queries.length];
    
            Map<String, Integer> origin = new HashMap<>();
            Map<String, Integer> lower = new HashMap<>();
            Map<String, Integer> trans = new HashMap<>();
    
            for (int i = 0; i < wordlist.length; i++) {
                String word = wordlist[i];
                origin.putIfAbsent(word, i);
                lower.putIfAbsent(word.toLowerCase(), i);
                trans.putIfAbsent(transform(word), i);
            }
    
            for (int i = 0; i < queries.length; i++) {
                String query1 = queries[i];
                if (origin.containsKey(query1)) {
                    ans[i] = wordlist[origin.get(query1)];
                    continue;
                }
    
                String query2 = query1.toLowerCase();
                if (lower.containsKey(query2)) {
                    ans[i] = wordlist[lower.get(query2)];
                    continue;
                }
    
                String query3 = transform(query1);
                if (trans.containsKey(query3)) {
                    ans[i] = wordlist[trans.get(query3)];
                    continue;
                }
    
                ans[i] = "";
            }
    
            return ans;
        }
    
        private String transform(String s) {
            s = s.toLowerCase();
            StringBuilder sb = new StringBuilder();
            for (char c : s.toCharArray()) {
                if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                    sb.append('#');
                } else {
                    sb.append(c);
                }
            }
            return sb.toString();
        }
    }
    
  • 相关阅读:
    指针,数组,字符串的区别(高质量程序设计指南C++/C语言第7章)
    bitset初始化问题
    书籍
    编译器的工作过程
    C++函数传递指向指针的指针的应用
    程序员面试金典--二叉树的下一个结点
    程序员面试金典--对称的二叉树
    程序员面试金典--按之字形顺序打印二叉树
    程序员面试金典--阶乘尾零
    程序员面试金典--矩阵元素查找
  • 原文地址:https://www.cnblogs.com/mapoos/p/14567580.html
Copyright © 2011-2022 走看看