Skip to content

Commit 07c5c58

Browse files
committedJan 25, 2025
Add solution #748
1 parent 6f41fff commit 07c5c58

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@
275275
739|[Daily Temperatures](./0739-daily-temperatures.js)|Medium|
276276
746|[Min Cost Climbing Stairs](./0746-min-cost-climbing-stairs.js)|Easy|
277277
747|[Largest Number At Least Twice of Others](./0747-largest-number-at-least-twice-of-others.js)|Easy|
278+
748|[Shortest Completing Word](./0748-shortest-completing-word.js)|Easy|
278279
762|[Prime Number of Set Bits in Binary Representation](./0762-prime-number-of-set-bits-in-binary-representation.js)|Easy|
279280
784|[Letter Case Permutation](./0784-letter-case-permutation.js)|Medium|
280281
791|[Custom Sort String](./0791-custom-sort-string.js)|Medium|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 748. Shortest Completing Word
3+
* https://leetcode.com/problems/shortest-completing-word/
4+
* Difficulty: Easy
5+
*
6+
* Given a string licensePlate and an array of strings words, find the shortest completing
7+
* word in words.
8+
*
9+
* A completing word is a word that contains all the letters in licensePlate. Ignore numbers
10+
* and spaces in licensePlate, and treat letters as case insensitive. If a letter appears more
11+
* than once in licensePlate, then it must appear in the word the same number of times or more.
12+
*
13+
* For example, if licensePlate = "aBc 12c", then it contains letters 'a', 'b' (ignoring case),
14+
* and 'c' twice. Possible completing words are "abccdef", "caaacab", and "cbca".
15+
*
16+
* Return the shortest completing word in words. It is guaranteed an answer exists. If there
17+
* are multiple shortest completing words, return the first one that occurs in words.
18+
*/
19+
20+
/**
21+
* @param {string} licensePlate
22+
* @param {string[]} words
23+
* @return {string}
24+
*/
25+
var shortestCompletingWord = function(licensePlate, words) {
26+
const license = licensePlate.toLowerCase().replace(/[\d\s]+/g, '');
27+
const sortedWords = [...words].sort((a, b) => a.length - b.length);
28+
29+
for (const word of sortedWords) {
30+
let updatedLicense = license;
31+
32+
for (let i = 0; i < word.length; i++) {
33+
updatedLicense = updatedLicense.replace(word[i], '');
34+
if (!updatedLicense) {
35+
return word;
36+
}
37+
}
38+
}
39+
};

0 commit comments

Comments
 (0)
Please sign in to comment.