Skip to content

1813. Sentence Similarity III #670

Answered by kovatz
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can approach it by comparing the longest common prefix and suffix of both sentences. If the words in the remaining part of one sentence are completely contained within the other sentence (possibly empty), then the sentences can be considered similar.

Steps:

  1. Split both sentences into arrays of words.
  2. Use two pointers to compare the longest common prefix from the start of both arrays.
  3. Use another two pointers to compare the longest common suffix from the end of both arrays.
  4. After comparing the common prefix and suffix, if the remaining words in one of the sentences form an empty array (meaning they have all been matched), then the sentences are considered similar.

Let's implement this …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Oct 6, 2024
Maintainer Author

@kovatz
Comment options

kovatz Oct 6, 2024
Collaborator

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty hacktoberfest hacktoberfest hacktoberfest-accepted hacktoberfest accepted
2 participants