当前位置:网站首页>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
边栏推荐
- [set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
- Ffmpeg mix
- Internationalization and localization, dark mode and dark mode in compose
- Redis persistence principle
- AWS VPC
- Smart contract security audit company selection analysis and audit report resources download - domestic article
- 2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical
- [文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
- [set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
- 金仓KFS数据双向同步场景部署
猜你喜欢
[nlp] - brief introduction to the latest work of spark neural network
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
[dynamic programming] subsequence problem
A outsourcing boy's mid-2022 summary
Pdf editing tool movavi pdfchef 2022 direct download
2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical
Database management tool, querious direct download
Basic use of continuous integration server Jenkins
随机推荐
Redis persistence principle
sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
C primre plus Chapter 10 question 6 inverted array
[fxcg] market analysis today
Which Bluetooth headset is cost-effective? Four Bluetooth headsets with high cost performance are recommended
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
What's wrong with SD card data damage? How to recover SD card data damage
540. Single element in ordered array
解决bp中文乱码
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
Reptile exercise 02
Mongodb slow query optimization analysis strategy
How to use kotlin to improve productivity: kotlin tips
What functions need to be set after the mall system is built
7. Integrated learning
Which code editor is easy to use? Code editing software recommendation
Kubernetes source code analysis (I)
[set theory] Cartesian product (concept of Cartesian product | examples of Cartesian product | properties of Cartesian product | non commutativity | non associativity | distribution law | ordered pair
Prefix and (continuously updated)
AWS VPC