[LeetCode] 1813. Sentence Similarity III

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.

句子相似性 III。

一个句子是由一些单词与它们之间的单个空格组成,且句子的开头和结尾没有多余空格。比方说,"Hello World" ,"HELLO" ,"hello world hello world" 都是句子。每个单词都 只 包含大写和小写英文字母。

如果两个句子 sentence1 和 sentence2 ,可以通过往其中一个句子插入一个任意的句子(可以是空句子)而得到另一个句子,那么我们称这两个句子是 相似的 。比方说,sentence1 = "Hello my name is Jane" 且 sentence2 = "Hello Jane" ,我们可以往 sentence2 中 "Hello" 和 "Jane" 之间插入 "my name is" 得到 sentence1 。

给你两个句子 sentence1 和 sentence2 ,如果 sentence1 和 sentence2 是相似的,请你返回 true ,否则返回 false 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/sentence-similarity-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路一 - 双端队列dequeue

把两个句子 sentence1 和 sentence2 转化成 string array, 再把 array 中的每个 string 放到一个双端队列 dequeue 里。比较两个 dequeue 左边和右边相同的单词,如果单词一样,则弹出,统计两边一共弹出的单词数。如果一共弹出的单词数等于两个句子中较短的那个句子的单词数,则返回 true ,否则返回 false 。

复杂度

时间O(n)
空间O(n)

代码

Java实现

class Solution {
    public boolean areSentencesSimilar(String sentence1, String sentence2) {
        Deque<String> dq1 = new ArrayDeque<>(Arrays.asList(sentence1.split(" ")));
        Deque<String> dq2 = new ArrayDeque<>(Arrays.asList(sentence2.split(" ")));
        while (!dq1.isEmpty() && !dq2.isEmpty() && dq1.peekFirst().equals(dq2.peekFirst())) {
            dq1.pollFirst();
            dq2.pollFirst();
        }
        while (!dq1.isEmpty() && !dq2.isEmpty() && dq1.peekLast().equals(dq2.peekLast())) {
            dq1.pollLast();
            dq2.pollLast();
        }
        return dq1.isEmpty() || dq2.isEmpty();
    }
}

// deque

思路二 - 双指针

与思路一类似,分别从左边和右边比较两个 string array 的单词,只不过 dequeue 的做法是弹出相同的单词,统计的是弹出的单词数;双指针的做法是统计了指针移动的步数。

复杂度

时间O(n)
空间O(n)

代码

Java实现

class Solution {
    public boolean areSentencesSimilar(String sentence1, String sentence2) {
		String[] words1 = sentence1.split(" ");
		String[] words2 = sentence2.split(" ");
		// corner case
		if (words1.length > words2.length) {
			String[] temp = words1;
			words1 = words2;
			words2 = temp;
		}

		// normal case
		int n = words1.length;
		int m = words2.length;
		int i = 0;
		int j = 0;
		// compare from left
		while (i < words1.length && words1[i].equals(words2[i])) {
			i++;
		}
		// compare from right
		while (j < words1.length && words1[n - 1 - j].equals(words2[m - 1 - j])) {
			j++;
		}
		return i + j >= n;
    }
}
posted @ 2023-01-16 06:21  CNoodle  阅读(96)  评论(0编辑  收藏  举报