LeetCode: Check If String Is a Prefix of Array Solution

Accumulated concatenation and check

Approach

On each concatenation of words, compare with s

Implementation

1var isPrefixString = function (s, words) {
2 let res = false
3
4 words.reduce((acc, word) => {
5 acc += word
6 if (acc === s) res = true
7 return acc
8 }, "")
9
10 return res
11}

Comments

Loading comments...

Tags

leetcode

array

string

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Concatenation of Array

.concat() or spread operator

Previous Post

LeetCode: Remove Duplicates from Sorted List

Keep jump over next node until values are not equal

HoningJS

Search Posts