首页 > 后端开发 > php教程 > 句子相似度 III

句子相似度 III

Mary-Kate Olsen
发布: 2024-10-07 06:08:01
原创
894 人浏览过

Sentence Similarity III

1813. Sentence Similarity III

Difficulty: Medium

Topics: Array, Two Pointers, String

You are given two strings sentence1 and sentence2, each representing a sentence composed of words. A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of only uppercase and lowercase English characters.

Two sentences s1 and s2 are considered similar if it is possible to insert an arbitrary sentence (possibly empty) inside one of these sentences such that the two sentences become equal. Note that the inserted sentence must be separated from existing words by spaces.

For example,

  • s1 = "Hello Jane" and s2 = "Hello my name is Jane" can be made equal by inserting "my name is" between "Hello" and "Jane" in s1.
  • s1 = "Frog cool" and s2 = "Frogs are cool" are not similar, since although there is a sentence "s are" inserted into s1, it is not separated from "Frog" by a space.

Given two sentences sentence1 and sentence2, return true if sentence1 and sentence2 are similar. Otherwise, return false.

Example 1:

  • Input: sentence1 = "My name is Haley", sentence2 = "My Haley"
  • Output: true
  • Explanation: sentence2 can be turned to sentence1 by inserting "name is" between "My" and "Haley".

Example 2:

  • Input: sentence1 = "of", sentence2 = "A lot of words"
  • Output: false
  • Explanation: No single sentence can be inserted inside one of the sentences to make it equal to the other.

Example 3:

  • Input: sentence1 = "Eating right now", sentence2 = "Eating"
  • Output: true
  • Explanation: sentence2 can be turned to sentence1 by inserting "right now" at the end of the sentence.

Constraints:

  • 1 <= sentence1.length, sentence2.length <= 100
  • sentence1 and sentence2 consist of lowercase and uppercase English letters and spaces.
  • The words in sentence1 and sentence2 are separated by a single space.

Hint:

  1. One way to look at it is to find one sentence as a concatenation of a prefix and suffix from the other sentence.
  2. Get the longest common prefix between them and the longest common suffix.

Solution:

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 solution in PHP: 1813. Sentence Similarity III


/**

  • @param String $sentence1
  • @param String $sentence2
  • @return Boolean / function areSentencesSimilar($sentence1, $sentence2) { ... ... ... /*
    • go to ./solution.php */ }

// Test examples
$sentence1 = "My name is Haley";
$sentence2 = "My Haley";
echo areSentencesSimilar($sentence1, $sentence2) ? 'true' : 'false'; // Output: true

$sentence1 = "of";
$sentence2 = "A lot of words";
echo areSentencesSimilar($sentence1, $sentence2) ? 'true' : 'false'; // Output: false

$sentence1 = "Eating right now";
$sentence2 = "Eating";
echo areSentencesSimilar($sentence1, $sentence2) ? 'true' : 'false'; // Output: true
?>




Explanation:

  1. Splitting sentences into words: We use explode() to split the sentence strings into arrays of words.
  2. Common prefix comparison: We iterate over both arrays from the start and compare corresponding words. The loop continues as long as the words match.
  3. Common suffix comparison: We compare the words from the end of both arrays, again using a loop to check for matching words.
  4. Final check: After processing the common prefix and suffix, we check if the number of words that have been matched (prefix + suffix) covers all the words in the shorter sentence. If so, the sentences can be considered similar.

Time Complexity:

  • The time complexity is O(n + m), where n and m are the lengths of sentence1 and sentence2 respectively. This is because we process the words in both sentences only once while checking the common prefix and suffix.

This solution should work efficiently given the constraints.

Contact Links

Wenn Sie diese Serie hilfreich fanden, denken Sie bitte darüber nach, dem Repository einen Stern auf GitHub zu geben oder den Beitrag in Ihren bevorzugten sozialen Netzwerken zu teilen? Ihre Unterstützung würde mir sehr viel bedeuten!

Wenn Sie weitere hilfreiche Inhalte wie diesen wünschen, folgen Sie mir gerne:

  • LinkedIn
  • GitHub

以上是句子相似度 III的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板