• 문제 링크
Shortest Completing Word - LeetCode
Can you solve this real interview question? Shortest Completing Word - Given a string licensePlate and an array of strings words, find the shortest completing word in words. A completing word is a word that contains all the letters in licensePlate. Ignore
leetcode.com
• 풀이 코드
public class Solution {
public String shortestCompletingWord(String licensePlate, String[] words) {
int[] tgt = getFrequency(licensePlate);
String s = null;
for (String word : words) if ((s == null || word.length() < s.length()) && isCompleting(tgt, word)) s = word;
return s;
}
private int[] getFrequency(String s) {
int[] freq = new int[26];
for (char c : s.toLowerCase().toCharArray()) if (c >= 'a' && c <= 'z') freq[c - 'a']++;
return freq;
}
private boolean isCompleting(int[] tgt, String word) {
int[] freq = getFrequency(word);
for (int i = 0; i < 26; i++) if (freq[i] < tgt[i]) return false;
return true;
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 762. Prime Number of Set Bits in Binary Representation - Java (0) | 2026.10.11 |
|---|---|
| [LeetCode] 747. Largest Number At Least Twice of Others - Java (0) | 2026.10.09 |
| [LeetCode] 746. Min Cost Climbing Stairs - Java (0) | 2026.10.08 |
| [LeetCode] 744. Find Smallest Letter Greater Than Target - Java (0) | 2026.10.07 |
| [LeetCode] 733. Flood Fill - Java (0) | 2026.10.06 |
댓글