From 3cf758425c79382899b619ca01f9904bd34dc4a3 Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Thu, 29 Dec 2022 16:56:51 -0800 Subject: [PATCH 1/2] solve 1078. Occurrences After Bigram --- 1078.occurrences-after-bigram.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1078.occurrences-after-bigram.js 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 From 474436f8a29b52826f403cd891e954669ea24439 Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Thu, 29 Dec 2022 16:57:28 -0800 Subject: [PATCH 2/2] test function findOcurrences --- findOcurrences.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 findOcurrences.test.js 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