LeetCode: Sentence Similarity III Solution

1/**
2 * @param {string} sentence1
3 * @param {string} sentence2
4 * @return {boolean}
5 */
6var areSentencesSimilar = function (sentence1, sentence2) {
7 const words1 = sentence1.split(" ")
8 const words2 = sentence2.split(" ")
9 while (words1.length > 0 && words2.length > 0 && words1[0] === words2[0]) {
10 words1.shift()
11 words2.shift()
12 }
13 while (
14 words1.length > 0 &&
15 words2.length > 0 &&
16 words1.slice(-1)[0] === words2.slice(-1)[0]
17 ) {
18 words1.pop()
19 words2.pop()
20 }
21 return words1.length === 0 || words2.length === 0
22}

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: Determine Color Of A Chessboard Square

HoningJS

Search Posts