当前位置:网站首页>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

边栏推荐
- Which code editor is easy to use? Code editing software recommendation
- Dive into deep learning - 2.1 data operation & Exercise
- How to retrieve the password for opening word files
- 2022-02-14 (394. String decoding)
- Why should programmers learn microservice architecture if they want to enter a large factory?
- 2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
- Solve BP Chinese garbled code
- Redis persistence principle
- Learning practice: comprehensive application of cycle and branch structure (I)
- GFS distributed file system (it's nice to meet it alone)
猜你喜欢

跨境电商多商户系统怎么选

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

When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error

Jincang KFS data bidirectional synchronization scenario deployment

2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank

Basic use of continuous integration server Jenkins

使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found

使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭

Asp access teaching management system design finished product

消息队列(MQ)介绍
随机推荐
Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found
Smart contract security audit company selection analysis and audit report resources download - domestic article
[fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments
How to use kotlin to improve productivity: kotlin tips
[BMZCTF-pwn] 20-secret_ file
Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
Redraw and reflow
[dynamic programming] subsequence problem
540. Single element in ordered array
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
vulnhub HA: Natraj
Employee attendance management system based on SSM
Human resource management system based on JSP
2022 new examination questions for the main principals of hazardous chemical business units and examination skills for the main principals of hazardous chemical business units
Dive Into Deep Learning——2.1数据操作&&练习
AWS VPC
FFMpeg example
[fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
金仓数据库KingbaseES 插件kdb_exists_expand