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

边栏推荐
- Basic types of data in TS
- Joint set search: merge intervals and ask whether two numbers are in the same set
- 使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
- 跨境电商多商户系统怎么选
- C language series - Section 3 - functions
- RSRS index timing and large and small disc rotation
- Why should programmers learn microservice architecture if they want to enter a large factory?
- Joint search set: the number of points in connected blocks (the number of points in a set)
- 解决bp中文乱码
- 2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
猜你喜欢

Integration of Android high-frequency interview questions (including reference answers)

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

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

redis 持久化原理

C language series - Section 3 - functions

4 years of experience to interview test development, 10 minutes to end, ask too
![[Thesis Writing] how to write the overall design of JSP tourism network](/img/02/841e8870c2ef871c182b9bb8252a83.jpg)
[Thesis Writing] how to write the overall design of JSP tourism network

The latest activation free version of Omni toolbox

GFS分布式文件系统(光是遇见已经很美好了)

Feature_selection
随机推荐
Joint set search: merge intervals and ask whether two numbers are in the same set
Kubernetes源码分析(一)
[set theory] binary relation (example of binary relation on a | binary relation on a)
[software testing-6] & Test Management
FISCO bcos zero knowledge proof Fiat Shamir instance source code
How to retrieve the password for opening word files
Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
Why should programmers learn microservice architecture if they want to enter a large factory?
Ffmpeg mix
FFMpeg example
vulnhub HA: Natraj
2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
Kubernetes source code analysis (I)
[fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
Design and implementation of JSP logistics center storage information management system
Factor stock selection scoring model
The latest activation free version of Omni toolbox
stm32逆向入门
What's wrong with SD card data damage? How to recover SD card data damage
Interface in TS