avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

1813. Sentence similarity III

  2024-01-01        
字数统计: 625字   |   阅读时长: 3min
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
One sentence is composed of a single space between some words and them,And there is no excess space at the beginning and end of the sentence。for example,"Hello World" ,"HELLO" ,"hello world hello world" All sentences。Each word Only Including uppercase and lowercase English letters。

If two sentences sentence1 and sentence2 ,You can insert an arbitrary sentence by one of the sentences(Can be an empty sentence)And get another sentence,Then we call these two sentences similar 。for example,sentence1 = "Hello my name is Jane" and sentence2 = "Hello Jane" ,We can go sentence2 middle "Hello" and "Jane" Insert an intercourse "my name is" get sentence1 。

Give you two sentences sentence1 and sentence2 ,if sentence1 and sentence2 是similar,Please return true ,Otherwise, return false 。



Exemplary example 1:

enter:sentence1 = "My name is Haley", sentence2 = "My Haley"
Output:true
explain:Be able to sentence2 middle "My" and "Haley" Insert an intercourse "name is" ,get sentence1 。
Exemplary example 2:

enter:sentence1 = "of", sentence2 = "A lot of words"
Output:false
explain:没法往这Two sentencesmiddle的一个句子Only插入一个句子就get另一个句子。
Exemplary example 3:

enter:sentence1 = "Eating right now", sentence2 = "Eating"
Output:true
explain:Be able to sentence2 Insert at the end "right now" get sentence1 。
Exemplary example 4:

enter:sentence1 = "Luky", sentence2 = "Lucccky"
Output:false


hint:

1 <= sentence1.length, sentence2.length <= 100
sentence1 and sentence2 都Only包含大小写英文字母and空格。
sentence1 and sentence2 middle的单词都Only由单个空格隔开。

My solution:

What I think is to make up the judgment of the character on the left or the right, and then delete it,
但是做的过程middle耐心没了,Feeling is a proper footing。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def areSentencesSimilar(self, sentence1: str, sentence2: str) -> bool:
list1 = sentence1.split(' ')
list2 = sentence2.split(' ')

if len(list1) < len(list2):
temp = list1
list1 = list2
list2 = temp
ans=copy.deepcopy(list2)
for i in list2:
for index, j in enumerate(list1):
if i == j and (index == 0 or index == len(list1) - 1):
ans.remove(i)
if not ans:
return True
else:
return False

Official solution:

According to the meaning,Two sentences sentence1 and sentence2,if是similar,那么这Two sentences按空格分割get的字符串数组 words1
and words2,一定能通过往其middle一个字符串数组middle插入某个字符串数组(Can be empty),get另一个字符串数组。这个验证可以通过Double pointer完成。
i Indicates that the two string array starts from left,At most i The string of the string is the same。
j It means that the remaining string array starts from right,At most j The string of the string is the same。
if i+j It happens to be the length of a string array,那么原字符串就是similar。

1
2
3
4
5
6
7
8
9
10
11
class Solution:
def areSentencesSimilar(self, sentence1: str, sentence2: str) -> bool:
words1 = sentence1.split()
words2 = sentence2.split()
i, j = 0, 0
while i < len(words1) and i < len(words2) and words1[i] == words2[i]:
i += 1
while j < len(words1) - i and j < len(words2) - i and words1[-j - 1] == words2[-j - 1]:
j += 1
return i + j == min(len(words1), len(words2))

  • Python
  • Double pointer
  • unsolved

扫一扫,分享到微信

微信分享二维码
1801-1803 Liech buckle novice!
1817. Find the number of users of users active One question daily
目录
  1. 1. My solution:
  2. 2. Official solution:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号