LeetCode: Reverse Prefix of Word Solution
Substring manipulatingApproach
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 === -14 ? word5 : word6 .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
Classic problem
Previous Post
LeetCode: Relative Ranks
Sometimes, LeetCode difficulty is not reliable