package com.fishercoder.solutions;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 966. Vowel Spellchecker
 *
 * 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.
 *
 * */

public class _966 {

    public static class Solution1 {
        public String[] spellchecker(String[] wordlist, String[] queries) {
            Map<String, String> caseMap = new HashMap<>();
            Set<String> set = new HashSet<>();

            // Case Part Mapping
            for (String word : wordlist) {
                if (!caseMap.containsKey(word.toLowerCase())) {
                    caseMap.put(word.toLowerCase(), word);
                }

                set.add(word);
            }

            // Vowel Part Mapping
            Map<String, String> vowelMap = new HashMap<>();
            for (String word : wordlist) {
                String genericVal = makeGenericVowel(word);
                if (!vowelMap.containsKey(genericVal)) {
                    vowelMap.put(genericVal, word);
                }
            }

            String[] ans = new String[queries.length];

            for (int i = 0; i < queries.length; i++) {
                if (set.contains(queries[i])) {
                    ans[i] = queries[i];
                } else if (caseMap.containsKey(queries[i].toLowerCase())) {
                    ans[i] = caseMap.get(queries[i].toLowerCase());
                } else if (vowelMap.containsKey(makeGenericVowel(queries[i]))) {
                    ans[i] = vowelMap.get(makeGenericVowel(queries[i]));
                } else {
                    ans[i] = "";
                }
            }

            return ans;
        }

        private String makeGenericVowel(String s) {
            String vowel = "aeiou";
            char[] ch = s.toLowerCase().toCharArray();
            for (int i = 0; i < ch.length; i++) {
                if (vowel.indexOf(ch[i]) != -1) {
                    ch[i] = '#';
                }
            }

            return String.valueOf(ch);
        }
    }
}

@@ -130,2182 +130,8 @@
 t;%0A%0A
-/**%0A * 966. Vowel Spellchecker%0A *%0A * Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word.%0A *%0A * For a given query word, the spell checker handles two categories of spelling mistakes:%0A *%0A * Capitalization: If the query matches a word in the wordlist (case-insensitive), then the query word is returned with%0A * the same case as the case in the wordlist.%0A * Example: wordlist = %5B%22yellow%22%5D, query = %22YellOw%22: correct = %22yellow%22%0A * Example: wordlist = %5B%22Yellow%22%5D, query = %22yellow%22: correct = %22Yellow%22%0A * Example: wordlist = %5B%22yellow%22%5D, query = %22yellow%22: correct = %22yellow%22%0A *%0A * Vowel Errors: If after replacing the vowels ('a', 'e', 'i', 'o', 'u') of the query word with any vowel individually,%0A * it matches a word in the wordlist (case-insensitive), then the query word is returned with the same case as the%0A * match in the wordlist.%0A * Example: wordlist = %5B%22YellOw%22%5D, query = %22yollow%22: correct = %22YellOw%22%0A * Example: wordlist = %5B%22YellOw%22%5D, query = %22yeellow%22: correct = %22%22 (no match)%0A * Example: wordlist = %5B%22YellOw%22%5D, query = %22yllw%22: correct = %22%22 (no match)%0A *%0A * In addition, the spell checker operates under the following precedence rules:%0A *%0A * When the query exactly matches a word in the wordlist (case-sensitive), you should return the same word back.%0A * When the query matches a word up to capitlization, you should return the first such match in the wordlist.%0A * When the query matches a word up to vowel errors, you should return the first such match in the wordlist.%0A * If the query has no matches in the wordlist, you should return the empty string.%0A *%0A * Given some queries, return a list of words answer, where answer%5Bi%5D is the correct word for query = queries%5Bi%5D.%0A *%0A * Example 1:%0A *%0A * Input: wordlist = %5B%22KiTe%22,%22kite%22,%22hare%22,%22Hare%22%5D, queries = %5B%22kite%22,%22Kite%22,%22KiTe%22,%22Hare%22,%22HARE%22,%22Hear%22,%22hear%22,%22keti%22,%22keet%22,%22keto%22%5D%0A * Output: %5B%22kite%22,%22KiTe%22,%22KiTe%22,%22Hare%22,%22hare%22,%22%22,%22%22,%22KiTe%22,%22%22,%22KiTe%22%5D%0A *%0A * Note:%0A *%0A * 1 %3C= wordlist.length %3C= 5000%0A * 1 %3C= queries.length %3C= 5000%0A * 1 %3C= wordlist%5Bi%5D.length %3C= 7%0A * 1 %3C= queries%5Bi%5D.length %3C= 7%0A * All strings in wordlist and queries consist only of english letters.%0A *%0A * */%0A%0A
 publ

refactor 966