LeetCode: Reverse Prefix of Word Solution

Substring manipulating

Approach

If there is no index of

ch
in
word
, skip

Everything else is straight forward

Implementation

1var reversePrefix = function (word, ch) {
2 const indexOfCh = word.indexOf(ch)
3 return indexOfCh === -1
4 ? word
5 : word
6 .substring(0, indexOfCh + 1)
7 .split("")
8 .reverse()
9 .join("") + word.substring(indexOfCh + 1)
10}

Comments

Loading comments...

Tags

leetcode

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: Binary Search

Sep 13, 2021

Classic problem

Previous Post

LeetCode: Relative Ranks

Sep 10, 2021

Sometimes, LeetCode difficulty is not reliable

HoningJS

Search Posts