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
22 changes: 22 additions & 0 deletions 1961.check-if-string-is-a-prefix-of-array.js
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 17 additions & 0 deletions isPrefixString.test.js
Original file line number Diff line number Diff line change
@@ -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();
});