forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortest Completing Word.js
31 lines (25 loc) · 1.25 KB
/
Shortest Completing Word.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Runtime: 102 ms (Top 73.77%) | Memory: 44.2 MB (Top 95.08%)
var shortestCompletingWord = function(licensePlate, words) {
// Object to hold the shortest word that matches
var match = {'found':false, 'word':''};
// Char array to hold the upper case characters we want to match
var licensePlateChars = licensePlate.toUpperCase().replace(/[^A-Z]/g, '').split('');
words.forEach(function (word) {
// if we already have a match make sure that the word we are checking is shorter
if (!match.found || word.length < match.word.length) {
var replaceWord = word.toUpperCase();
// Loop over each character in the license plate and replace one at a time
// the key here is that replace will only replace 1 S even if there are 2
licensePlateChars.forEach(function (lChar) {
replaceWord = replaceWord.replace(lChar, '');
});
// We know the word works if the length of the word minus
// the length of chars equals the length of the new word
if (word.length - licensePlateChars.length === replaceWord.length) {
match.found = true;
match.word = word
}
}
});
return match.word;
};