-
Notifications
You must be signed in to change notification settings - Fork 3
/
1078-occurrences-after-bigram.js
72 lines (59 loc) · 2.09 KB
/
1078-occurrences-after-bigram.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
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
65
66
67
68
69
70
71
72
// 1078. Occurrences After Bigram
// https://leetcode.com/problems/occurrences-after-bigram/
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 48 ms, faster than 91.64% of JavaScript online submissions
// Memory Usage: 33.8 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {string} text
// * @param {string} first
// * @param {string} second
// * @return {string[]}
// */
// const findOcurrences = (text, first, second) =>
// text.split(' ').reduce((acc, curr, i, t) => {
// if (i < 2) return acc;
// if (first === t[i - 2] && second === t[i - 1]) acc.push(curr);
// return acc;
// }, []);
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 60 ms, faster than 30.87% of JavaScript online submissions
// Memory Usage: 33.1 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {string} text
// * @param {string} first
// * @param {string} second
// * @return {string[]}
// */
// const findOcurrences = (text, first, second) =>
// text
// .split(' ')
// .reduce(
// (acc, curr, idx, src) =>
// first === src[idx - 2] && second === src[idx - 1]
// ? acc.concat(curr)
// : acc,
// [],
// );
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 48 ms, faster than 91.28% of JavaScript online submissions
// Memory Usage: 33.7 MB, less than 100.00% of JavaScript online submissions
/**
* @param {string} text
* @param {string} first
* @param {string} second
* @return {string[]}
*/
const findOcurrences = (text, first, second) =>
text
.split(' ')
.filter((_, idx, src) => first === src[idx - 2] && second === src[idx - 1]);
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import { deepStrictEqual } from 'assert';
deepStrictEqual(
findOcurrences('alice is a good girl she is a good student', 'a', 'good'),
['girl', 'student'],
);
deepStrictEqual(findOcurrences('we will we will rock you', 'we', 'will'), [
'we',
'rock',
]);