Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions 1078.occurrences-after-bigram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* URL of this problem
* https://leetcode.com/problems/occurrences-after-bigram/description/
*
* @param {string} text
* @param {string} first
* @param {string} second
* @return {string[]}
*/

var findOcurrences = function(text, first, second) {
const Words = text.split(" ");
const ThirdWords = [];

for (let i = 0; i < Words.length - 2; i++) {
const CurrFirst = Words[i];
const CurrSecond = Words[i + 1];
const CurrThird = Words[i + 2];

if (CurrFirst === first && CurrSecond === second) {
ThirdWords.push(CurrThird);
}
}

return ThirdWords;
};

module.exports = findOcurrences;
21 changes: 21 additions & 0 deletions findOcurrences.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const findOcurrences = require("./1078.occurrences-after-bigram");

test("Return an array of all the third words after first and second in its occurrence order", () => {
expect(findOcurrences("alice is a good girl she is a good student", "a", "good"))
.toEqual(["girl","student"]);
});

test("Return an empty array if the input text is empty", () => {
expect(findOcurrences("", "a", "good"))
.toEqual([]);
});

test("Return an empty array if the input first is empty", () => {
expect(findOcurrences("alice is a good girl she is a good student", "", "good"))
.toEqual([]);
});

test("Return an empty array if the input second is empty", () => {
expect(findOcurrences("alice is a good girl she is a good student", "a", ""))
.toEqual([]);
});