-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0925-long-pressed-name.js
More file actions
60 lines (50 loc) · 1.66 KB
/
0925-long-pressed-name.js
File metadata and controls
60 lines (50 loc) · 1.66 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
/**
* Long Pressed Name
* Time Complexity: O(N + M)
* Space Complexity: O(N + M)
*/
var isLongPressedName = function (name, typed) {
const createCharacterRuns = (inputStringForRLE) => {
const characterRunsList = [];
let primaryScanIndex = 0;
while (primaryScanIndex < inputStringForRLE.length) {
const currentRunCharacter = inputStringForRLE[primaryScanIndex];
let runLengthCount = 0;
let secondaryScanIndex = primaryScanIndex;
while (
secondaryScanIndex < inputStringForRLE.length &&
inputStringForRLE[secondaryScanIndex] === currentRunCharacter
) {
runLengthCount++;
secondaryScanIndex++;
}
characterRunsList.push([currentRunCharacter, runLengthCount]);
primaryScanIndex = secondaryScanIndex;
}
return characterRunsList;
};
const nameRLEStructure = createCharacterRuns(name);
const typedRLEStructure = createCharacterRuns(typed);
const nameRLELength = nameRLEStructure.length;
const typedRLELength = typedRLEStructure.length;
if (nameRLELength !== typedRLELength) {
return false;
}
let comparisonIndex = 0;
while (comparisonIndex < nameRLELength) {
const nameCurrentBlock = nameRLEStructure[comparisonIndex];
const typedCurrentBlock = typedRLEStructure[comparisonIndex];
const nameCharacterValue = nameCurrentBlock[0];
const nameCountValue = nameCurrentBlock[1];
const typedCharacterValue = typedCurrentBlock[0];
const typedCountValue = typedCurrentBlock[1];
if (
nameCharacterValue !== typedCharacterValue ||
nameCountValue > typedCountValue
) {
return false;
}
comparisonIndex++;
}
return true;
};