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

边栏推荐
- Contents of welder (primary) examination and welder (primary) examination in 2022
- 2022-02-14 (394. String decoding)
- 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
- Solve BP Chinese garbled code
- After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed
- Kingbasees plug-in KDB of Jincang database_ date_ function
- FuncS sh file not found when using the benchmarksql tool to test kingbases
- Reptile exercise 02
- Kubernetes源码分析(一)
- P35-P41 fourth_ context
猜你喜欢

Smart contract security audit company selection analysis and audit report resources download - domestic article

会员积分商城系统的功能介绍

A outsourcing boy's mid-2022 summary

Introduction of pointer variables in function parameters
![[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries](/img/56/386f0fd6553b8b9711e14c54705ae3.jpg)
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries

2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test

GFS distributed file system (it's nice to meet it alone)

JVM原理简介

Busycal latest Chinese version
![[NLP]—sparse neural network最新工作简述](/img/65/35ae0137f4030bdb2b0ab9acd85e16.png)
[NLP]—sparse neural network最新工作简述
随机推荐
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
MongoDB 慢查询语句优化分析策略
Two drawing interfaces - 1 Matlab style interface
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
怎么用Kotlin去提高生产力:Kotlin Tips
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
Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute
多板块轮动策略编写技巧----策略编写学习教材
Kubernetes source code analysis (I)
有道云笔记
Hj35 serpentine matrix
Xrandr modify resolution and refresh rate
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
[set theory] ordered pair (ordered pair | ordered triple | ordered n ancestor)
2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
Solve BP Chinese garbled code
The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
220214c language learning diary
540. Single element in ordered array