From 87203f3b0f7b3b6963e8bac2a176fbf8dd418f40 Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Sat, 14 Jan 2023 15:28:49 -0800 Subject: [PATCH 1/2] solve 1961. Check If String Is a Prefix of Array --- 1961.check-if-string-is-a-prefix-of-array.js | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 1961.check-if-string-is-a-prefix-of-array.js diff --git a/1961.check-if-string-is-a-prefix-of-array.js b/1961.check-if-string-is-a-prefix-of-array.js new file mode 100644 index 00000000..92d575e7 --- /dev/null +++ b/1961.check-if-string-is-a-prefix-of-array.js @@ -0,0 +1,22 @@ +/* URL of this problem + * https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/description/ + * + * @param {string} s + * @param {string[]} words + * @return {boolean} + */ + +var isPrefixString = function(s, words) { + let str = ""; + + for (let i = 0; i < words.length; i++) { + str += words[i]; + + if (s === str) { + return true; + } + } + return false; +}; + +module.exports = isPrefixString; \ No newline at end of file From cda17bb5efa624ed249a861fb04807bfd3710ff4 Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Sat, 14 Jan 2023 15:29:11 -0800 Subject: [PATCH 2/2] test function isPrefixString --- isPrefixString.test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 isPrefixString.test.js diff --git a/isPrefixString.test.js b/isPrefixString.test.js new file mode 100644 index 00000000..cb81467f --- /dev/null +++ b/isPrefixString.test.js @@ -0,0 +1,17 @@ +const isPrefixString = require("../jest-test/1961.check-if-string-is-a-prefix-of-array"); + +test("Return true if the argument s can be made by concatinating the first k strings in the argument words", () => { + expect(isPrefixString("iloveleetcode", ["i","love","leetcode","apples"])).toBeTruthy(); +}); + +test("Return false if the argument s can NOT be made by concatinating the first k strings in the argument words", () => { + expect(isPrefixString("iloveleetcode", ["apples","i","love","leetcode"])).toBeFalsy(); +}); + +test("Return false if the argument s is empty", () => { + expect(isPrefixString("", ["i","love","leetcode","apples"])).toBeFalsy(); +}); + +test("Return false if the argument words empty", () => { + expect(isPrefixString("iloveleetcode", [])).toBeFalsy(); +}); \ No newline at end of file