Skip to content

Commit 1250f0d

Browse files
committed
Runtime: 102 ms (Top 73.77%) | Memory: 44.2 MB (Top 95.08%)
1 parent 9e3a1e2 commit 1250f0d

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
1+
// Runtime: 102 ms (Top 73.77%) | Memory: 44.2 MB (Top 95.08%)
12
var shortestCompletingWord = function(licensePlate, words) {
2-
3+
34
// Object to hold the shortest word that matches
45
var match = {'found':false, 'word':''};
5-
6+
67
// Char array to hold the upper case characters we want to match
78
var licensePlateChars = licensePlate.toUpperCase().replace(/[^A-Z]/g, '').split('');
8-
9+
910
words.forEach(function (word) {
1011
// if we already have a match make sure that the word we are checking is shorter
1112
if (!match.found || word.length < match.word.length) {
1213
var replaceWord = word.toUpperCase();
13-
14+
1415
// Loop over each character in the license plate and replace one at a time
1516
// the key here is that replace will only replace 1 S even if there are 2
1617
licensePlateChars.forEach(function (lChar) {
1718
replaceWord = replaceWord.replace(lChar, '');
1819
});
19-
20-
// We know the word works if the length of the word minus
20+
21+
// We know the word works if the length of the word minus
2122
// the length of chars equals the length of the new word
2223
if (word.length - licensePlateChars.length === replaceWord.length) {
2324
match.found = true;
2425
match.word = word
2526
}
2627
}
2728
});
28-
29+
2930
return match.word;
30-
};
31+
};

0 commit comments

Comments
 (0)