1
var shortestCompletingWord = function (licensePlate, words) {
2
// Object to hold the shortest word that matches
3
var match = { found: false, word: "" };
4

5
// Char array to hold the upper case characters we want to match
6
var licensePlateChars = licensePlate
7
.toUpperCase()
8
.replace(/[^A-Z]/g, "")
9
.split("");
10

11
words.forEach(function (word) {
12
// if we already have a match make sure that the word we are checking is shorter
13
if (!match.found || word.length < match.word.length) {
14
var replaceWord = word.toUpperCase();
15

16
// Loop over each character in the license plate and replace one at a time
17
// the key here is that replace will only replace 1 S even if there are 2
18
licensePlateChars.forEach(function (lChar) {
19
replaceWord = replaceWord.replace(lChar, "");
20
});
21

22
// We know the word works if the length of the word minus
23
// the length of chars equals the length of the new word
24
if (word.length - licensePlateChars.length === replaceWord.length) {
25
match.found = true;
26
match.word = word;
27
}
28
}
29
});
30

31
return match.word;
32
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0