-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0809-expressive-words.js
More file actions
64 lines (55 loc) · 1.67 KB
/
0809-expressive-words.js
File metadata and controls
64 lines (55 loc) · 1.67 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Expressive Words
* Time Complexity: O(W * (N + K_max))
* Space Complexity: O(1)
*/
var expressiveWords = function (sourceInputString, wordsToExamine) {
let totalStretchyCount = 0;
for (const currentWordConsidered of wordsToExamine) {
if (checkStretchy(sourceInputString, currentWordConsidered)) {
totalStretchyCount++;
}
}
return totalStretchyCount;
};
function checkStretchy(mainSource, mainQuery) {
let sourceStringPointer = 0;
let queryWordPointer = 0;
while (
sourceStringPointer < mainSource.length &&
queryWordPointer < mainQuery.length
) {
if (mainSource[sourceStringPointer] !== mainQuery[queryWordPointer]) {
return false;
}
const currentGroupCharacter = mainSource[sourceStringPointer];
let nextSourcePosition = sourceStringPointer;
while (
nextSourcePosition < mainSource.length &&
mainSource[nextSourcePosition] === currentGroupCharacter
) {
nextSourcePosition++;
}
const sourceGroupLength = nextSourcePosition - sourceStringPointer;
sourceStringPointer = nextSourcePosition;
let nextQueryPosition = queryWordPointer;
while (
nextQueryPosition < mainQuery.length &&
mainQuery[nextQueryPosition] === currentGroupCharacter
) {
nextQueryPosition++;
}
const queryGroupLength = nextQueryPosition - queryWordPointer;
queryWordPointer = nextQueryPosition;
if (queryGroupLength > sourceGroupLength) {
return false;
}
if (sourceGroupLength > queryGroupLength && sourceGroupLength < 3) {
return false;
}
}
return (
sourceStringPointer === mainSource.length &&
queryWordPointer === mainQuery.length
);
}