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 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