748. Shortest Completing Word
Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate Here, for letters we ignore case. For example, "P" on the licensePlate still matches "p" on the word. It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array. The license plate might have the same letter occurring multiple times. For example, given a licensePlate of "PP", the word "pair" does not complete the licensePlate, but the word "supper" does. Example 1: Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"] Output: "steps" Explanation: The smallest length word that contains the letters "S", "P", "S", and "T". Note that the answer is not "step", because the letter "s" must occur in the word twice. Also note that we ignored case for the purposes of comparing whether a letter exists in the word. Example 2: Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"] Output: "pest" Explanation: There are 3 smallest length words that contains the letters "s". We return the one that occurred first. // correct class Solution { public String shortestCompletingWord(String licensePlate, String[] words) { String target = licensePlate.toLowerCase(); int [] charMap = new int[26]; // Construct the character map for(int i = 0 ; i < target.length(); i++){ if(Character.isLetter(target.charAt(i))) charMap[target.charAt(i) - 'a']++; } int minLength = Integer.MAX_VALUE; String result = null; for (int i = 0; i < words.length; i++){ String word = words[i].toLowerCase(); if(matches(word, charMap) && word.length() < minLength) { minLength = word.length(); result = words[i]; } } return result; } private boolean matches(String word, int[] charMap){ int [] targetMap = new int[26]; for(int i = 0; i < word.length(); i++){ if(Character.isLetter(word.charAt(i))) targetMap[word.charAt(i) - 'a']++; } for(int i = 0; i < 26; i++){ if(charMap[i]!=0 && targetMap[i]<charMap[i]) return false; } return true; } } // wrong result class Solution { public String shortestCompletingWord(String licensePlate, String[] words) { // suppose my sorting works on the rule that shorter comes first and the original order is not changed //Arrays.sort((a, b) -> a.length() != b.length() ? a.length().compareTo(b.length()) : ) // how to get the index in the origial array ?? // sort the dict by length , but there are some words with the same length, we might not be able to guarentee the // original order , or if we have some custom sorting method that still guarentrees the order of the word with the same // length // use a hashamp to record the char,and freq // check each word in the dict // use anothe hashmap to record the char and freq, // if every char in the map 1 has found same freq or even bigger freq in map 2 , retunr the word HashMap<Character, Integer> map1 = new HashMap<>(); for(char c : licensePlate.toCharArray()){ if(Character.isDigit(c)) continue; c = Character.toLowerCase(c); if(!map1.containsKey(c)){ map1.put(c, 1); }else{ map1.put(c, map1.get(c) + 1); // could be more concise with the map,get default } } // traverse the words for(String word : words){ boolean flag = true; HashMap<Character, Integer> map2 = new HashMap<>(); for(char c : word.toCharArray()){ if(!map2.containsKey(c)){ map2.put(c, 1); }else{ map2.put(c, map2.get(c) + 1); } } // compare the two maps for(Map.Entry<Character, Integer> entry : map1.entrySet()){ // map.entrySet() char c = entry.getKey(); int freq = entry.getValue(); // still not sure the syntax if(!map2.containsKey(c)){ flag = false; }else{ if(map2.get(c) < freq) flag = false; } } if(flag) return word; } return ""; } }
posted on 2018-11-08 02:34 猪猪🐷 阅读(171) 评论(0) 收藏 举报
浙公网安备 33010602011771号