|
| 1 | +/* |
| 2 | + * LeetCode Problem: Vowel Spellchecker |
| 3 | + * https://leetcode.com/problems/vowel-spellchecker/ |
| 4 | + * |
| 5 | + * Given a wordlist and a list of queries, return a list of words from wordlist |
| 6 | + * for each query based on the following rules in order of priority: |
| 7 | + * 1. Exact match |
| 8 | + * 2. Case-insensitive match |
| 9 | + * 3. Vowel error match (replace vowels with any vowel) |
| 10 | + * |
| 11 | + * Approach: |
| 12 | + * - Store exact words in a HashSet for O(1) look-up. |
| 13 | + * - Build two maps: |
| 14 | + * 1. caseInsensitiveMap: maps lowercase words to the original word (first occurrence). |
| 15 | + * 2. vowelErrorMap: maps vowel-masked lowercase words (vowels replaced by '*') to the original word (first occurrence). |
| 16 | + * - For each query, try exact match → case-insensitive match → vowel error match → "" (if no match). |
| 17 | + * |
| 18 | + * Time Complexity: O(N + Q * L) |
| 19 | + * - N = wordlist length, Q = queries length, L = average word length |
| 20 | + * - O(N * L) to process the wordlist and build maps. |
| 21 | + * - O(Q * L) to process queries. |
| 22 | + * |
| 23 | + * Space Complexity: O(N * L) |
| 24 | + * - Storing words in HashSet and maps. |
| 25 | + */ |
| 26 | + |
| 27 | +import java.util.*; |
| 28 | + |
| 29 | +public class Solution { |
| 30 | + public String[] spellchecker(String[] wordlist, String[] queries) { |
| 31 | + Set<String> exactWords = new HashSet<>(Arrays.asList(wordlist)); |
| 32 | + Map<String, String> caseInsensitiveMap = new HashMap<>(); |
| 33 | + Map<String, String> vowelErrorMap = new HashMap<>(); |
| 34 | + |
| 35 | + for (String word : wordlist) { |
| 36 | + String wordLower = word.toLowerCase(); |
| 37 | + caseInsensitiveMap.putIfAbsent(wordLower, word); |
| 38 | + |
| 39 | + String wordVowelMasked = maskVowels(wordLower); |
| 40 | + vowelErrorMap.putIfAbsent(wordVowelMasked, word); |
| 41 | + } |
| 42 | + |
| 43 | + String[] ans = new String[queries.length]; |
| 44 | + for (int i = 0; i < queries.length; i++) { |
| 45 | + String query = queries[i]; |
| 46 | + |
| 47 | + if (exactWords.contains(query)) { |
| 48 | + ans[i] = query; |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + String queryLower = query.toLowerCase(); |
| 53 | + if (caseInsensitiveMap.containsKey(queryLower)) { |
| 54 | + ans[i] = caseInsensitiveMap.get(queryLower); |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + String queryVowelMasked = maskVowels(queryLower); |
| 59 | + if (vowelErrorMap.containsKey(queryVowelMasked)) { |
| 60 | + ans[i] = vowelErrorMap.get(queryVowelMasked); |
| 61 | + } else { |
| 62 | + ans[i] = ""; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return ans; |
| 67 | + } |
| 68 | + |
| 69 | + private String maskVowels(String str) { |
| 70 | + StringBuilder sb = new StringBuilder(); |
| 71 | + for (char c : str.toCharArray()) { |
| 72 | + if ("aeiou".indexOf(c) != -1) |
| 73 | + sb.append('*'); |
| 74 | + else |
| 75 | + sb.append(c); |
| 76 | + } |
| 77 | + return sb.toString(); |
| 78 | + } |
| 79 | +} |
0 commit comments