|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 748. Shortest Completing Word |
5 |
| - * |
6 |
| - * Find the minimum length word from a given dictionary words, |
7 |
| - * which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate |
8 |
| - * |
9 |
| - * Here, for letters we ignore case. For example, "P" on the licensePlate still matches "p" on the word. |
10 |
| - * It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array. |
11 |
| - * The license plate might have the same letter occurring multiple times. For example, |
12 |
| - * given a licensePlate of "PP", the word "pair" does not complete the licensePlate, but the word "supper" does. |
13 |
| -
|
14 |
| - Example 1: |
15 |
| - Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"] |
16 |
| - Output: "steps" |
17 |
| - Explanation: The smallest length word that contains the letters "S", "P", "S", and "T". |
18 |
| - Note that the answer is not "step", because the letter "s" must occur in the word twice. |
19 |
| - Also note that we ignored case for the purposes of comparing whether a letter exists in the word. |
20 |
| -
|
21 |
| - Example 2: |
22 |
| - Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"] |
23 |
| - Output: "pest" |
24 |
| - Explanation: There are 3 smallest length words that contains the letters "s". |
25 |
| - We return the one that occurred first. |
26 |
| -
|
27 |
| - Note: |
28 |
| - licensePlate will be a string with length in range [1, 7]. |
29 |
| - licensePlate will contain digits, spaces, or letters (uppercase or lowercase). |
30 |
| - words will have a length in the range [10, 1000]. |
31 |
| - Every words[i] will consist of lowercase letters, and have length in range [1, 15]. |
32 |
| -
|
33 |
| - */ |
34 | 3 | public class _748 {
|
35 | 4 |
|
36 |
| - public static class Solution1 { |
37 |
| - public String shortestCompletingWord(String licensePlate, String[] words) { |
38 |
| - int[] counts = new int[26]; |
39 |
| - for (char c : licensePlate.toCharArray()) { |
40 |
| - if (Character.isAlphabetic(c)) { |
41 |
| - counts[Character.toLowerCase(c) - 'a']++; |
| 5 | + public static class Solution1 { |
| 6 | + public String shortestCompletingWord(String licensePlate, String[] words) { |
| 7 | + int[] counts = new int[26]; |
| 8 | + for (char c : licensePlate.toCharArray()) { |
| 9 | + if (Character.isAlphabetic(c)) { |
| 10 | + counts[Character.toLowerCase(c) - 'a']++; |
| 11 | + } |
| 12 | + } |
| 13 | + String result = ""; |
| 14 | + for (String word : words) { |
| 15 | + if (isComplete(word, counts)) { |
| 16 | + if (result.equals("")) { |
| 17 | + result = word; |
| 18 | + } else if (word.length() < result.length()) { |
| 19 | + result = word; |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + return result; |
42 | 24 | }
|
43 |
| - } |
44 |
| - String result = ""; |
45 |
| - for (String word : words) { |
46 |
| - if (isComplete(word, counts)) { |
47 |
| - if (result.equals("")) { |
48 |
| - result = word; |
49 |
| - } else if (word.length() < result.length()) { |
50 |
| - result = word; |
51 |
| - } |
52 |
| - } |
53 |
| - } |
54 |
| - return result; |
55 |
| - } |
56 | 25 |
|
57 |
| - private boolean isComplete(String word, int[] counts) { |
58 |
| - int[] tmp = counts.clone(); |
59 |
| - for (char c : word.toCharArray()) { |
60 |
| - if (tmp[c - 'a'] > 0) { |
61 |
| - tmp[c - 'a']--; |
62 |
| - } |
63 |
| - } |
64 |
| - for (int i : tmp) { |
65 |
| - if (i != 0) { |
66 |
| - return false; |
| 26 | + private boolean isComplete(String word, int[] counts) { |
| 27 | + int[] tmp = counts.clone(); |
| 28 | + for (char c : word.toCharArray()) { |
| 29 | + if (tmp[c - 'a'] > 0) { |
| 30 | + tmp[c - 'a']--; |
| 31 | + } |
| 32 | + } |
| 33 | + for (int i : tmp) { |
| 34 | + if (i != 0) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + } |
| 38 | + return true; |
67 | 39 | }
|
68 |
| - } |
69 |
| - return true; |
70 | 40 | }
|
71 |
| - } |
72 | 41 | }
|
0 commit comments