当前位置:网站首页>Leetcode simple question: check whether two string arrays are equal
Leetcode simple question: check whether two string arrays are equal
2022-07-03 04:31:00 【·Starry Sea】
subject
Here are two string arrays word1 and word2 . If two arrays represent the same string , return true ; otherwise , return false .
Array representation string Is made up of all the elements in the array According to the order The string formed by the connection .
Example 1:
Input :word1 = [“ab”, “c”], word2 = [“a”, “bc”]
Output :true
explain :
word1 The string represented is “ab” + “c” -> “abc”
word2 The string represented is “a” + “bc” -> “abc”
The two strings are the same , return true
Example 2:
Input :word1 = [“a”, “cb”], word2 = [“ab”, “c”]
Output :false
Example 3:
Input :word1 = [“abc”, “d”, “defg”], word2 = [“abcddefg”]
Output :true
Tips :
1 <= word1.length, word2.length <= 10^3
1 <= word1[i].length, word2[i].length <= 10^3
1 <= sum(word1[i].length), sum(word2[i].length) <= 10^3
word1[i] and word2[i] It's made up of lowercase letters
source : Power button (LeetCode)
Their thinking
The simplest way to solve this problem is to splice the elements in the array into sentences, and then compare whether the sentences are equal .
class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return ''.join(word1)==''.join(word2)

Of course, you can also traverse characters one by one .
class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
i=0
j=0
A=iter(word1[i])
B=iter(word2[i])
while True:
try:
s1=next(A)
except:
i+=1
if i==len(word1):
break
A=iter(word1[i])
s1=next(A)
try:
s2=next(B)
except:
j+=1
if j==len(word2):
break
B=iter(word2[j])
s2=next(B)
if s1!=s2:
return False
try:
next(A)
return False
except:
try:
iter(word1[i+1])
return False
except:
pass
try:
next(B)
return False
except:
try:
iter(word2[j+1])
return False
except:
pass
return True if s1==s2 else False

边栏推荐
- MySQL field userid comma separated save by userid query
- After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed
- 使用BENCHMARKSQL工具对kingbaseES执行灌数据提示无法找到JDBC driver
- Mount NFS in kubesphere
- Dive Into Deep Learning——2.1数据操作&&练习
- Kingbasees plug-in KDB of Jincang database_ database_ link
- MC Layer Target
- 解决bp中文乱码
- Library management system based on SSM
- Small program animation realizes the running lantern and animation object
猜你喜欢

Jincang KFS data bidirectional synchronization scenario deployment

Busycal latest Chinese version

Employee attendance management system based on SSM

Youdao cloud notes

Why should programmers learn microservice architecture if they want to enter a large factory?
![[fxcg] market analysis today](/img/ac/294368e3496a5b808b38833053ee81.jpg)
[fxcg] market analysis today

Human resource management system based on JSP

Two points -leetcode-540 A single element in an ordered array

Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected

After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
随机推荐
Priv app permission exception
540. Single element in ordered array
[fxcg] market analysis today
Employee attendance management system based on SSM
Factor stock selection scoring model
Pdf editing tool movavi pdfchef 2022 direct download
7. Integrated learning
Php+mysql registration landing page development complete code
JS multidimensional array to one-dimensional array
Writing skills of multi plate rotation strategy -- strategy writing learning materials
2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
Which code editor is easy to use? Code editing software recommendation
[set theory] inclusion exclusion principle (including examples of exclusion principle)
MC Layer Target
[set theory] binary relationship (binary relationship notation | binary relationship from a to B | number of binary relationships | example of binary relationship)
Youdao cloud notes
Matplotlib -- save graph
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
[BMZCTF-pwn] 20-secret_ file
How do you use lodash linking function- How do you chain functions using lodash?