diff --git a/1078.occurrences-after-bigram.js b/1078.occurrences-after-bigram.js new file mode 100644 index 00000000..cd6275dc --- /dev/null +++ b/1078.occurrences-after-bigram.js @@ -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; \ No newline at end of file diff --git a/findOcurrences.test.js b/findOcurrences.test.js new file mode 100644 index 00000000..5305e981 --- /dev/null +++ b/findOcurrences.test.js @@ -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([]); +}); \ No newline at end of file