当前位置:网站首页>Leetcode simple question: check whether the string is an array prefix
Leetcode simple question: check whether the string is an array prefix
2022-07-03 04:31:00 【·Starry Sea】
subject
Give you a string s And an array of strings words , Please judge s Is it words Of Prefix string .
character string s To be words Of Prefix string , Need to meet :s Can be words In front of k(k by Positive numbers ) Strings are concatenated in order to get , And k No more than words.length .
If s yes words Of Prefix string , return true ; otherwise , return false .
Example 1:
Input :s = “iloveleetcode”, words = [“i”,“love”,“leetcode”,“apples”]
Output :true
explain :
s Can be “i”、“love” and “leetcode” Connected to get .
Example 2:
Input :s = “iloveleetcode”, words = [“apples”,“i”,“love”,“leetcode”]
Output :false
explain :
Array prefixes cannot be concatenated to get s .
Tips :
1 <= words.length <= 100
1 <= words[i].length <= 20
1 <= s.length <= 1000
words[i] and s It's only made up of lowercase letters
source : Power button (LeetCode)
Their thinking
The premise of matching this question is words The number of all characters in must be greater than s, in addition ,s The characters in are one by one with words When the characters in match ,s At the end of the match , You can't just match words Half of an element in , Must happen to words Only when the whole word is matched can it be regarded as a successful match .
class Solution:
def isPrefixString(self, s: str, words: List[str]) -> bool:
temp=[len(words[0])] # Store the cumulative length of complete words
for i in words[1:]:
temp.append(len(i)+temp[-1])
S=''.join(words) # take words Link into sentences
if len(s)>len(S): # If words The number of all characters in is less than s Then it will not match successfully
return False
for i in range(len(s)):
if s[i]!=S[i]:
return False
return True if i+1 in set(temp) else False # If it happens to match with a complete word, then the match is successful

Of course, you can also check character by character , This also avoids calculating complete words .
class Solution:
def isPrefixString(self, s: str, words: List[str]) -> bool:
A=iter(s)
i=0
B=iter(words[i])
flag=0 # Interrupt flag
while True:
try:
s1=next(A)
except:
flag=0
break
try:
s2=next(B)
except:
i+=1
if i==len(words):
flag=1
break
B=iter(words[i])
s2=next(B)
if s1!=s2:
return False
if flag: #words There are not enough characters in
return False
else:
try: #words A word in does not match completely
next(B)
return False
except:
return True

边栏推荐
- GFS分布式文件系统(光是遇见已经很美好了)
- Two points -leetcode-540 A single element in an ordered array
- sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
- 金仓数据库KingbaseES 插件kdb_date_function
- How to use kotlin to improve productivity: kotlin tips
- [set theory] set identities (idempotent law | exchange law | combination law | distribution rate | De Morgan law | absorption rate | zero law | identity | exclusion law | contradiction law | complemen
- xrandr修改分辨率與刷新率
- How to choose cross-border e-commerce multi merchant system
- redis 持久化原理
- 7. Integrated learning
猜你喜欢

Which Bluetooth headset is cost-effective? Four Bluetooth headsets with high cost performance are recommended

When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully

Youdao cloud notes
![[fxcg] market analysis today](/img/ac/294368e3496a5b808b38833053ee81.jpg)
[fxcg] market analysis today

4 years of experience to interview test development, 10 minutes to end, ask too

Arthas watch grabs a field / attribute of the input parameter

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

Some information about the developer environment in Chengdu

vulnhub HA: Natraj

arthas watch 抓取入参的某个字段/属性
随机推荐
Youdao cloud notes
IPhone x forgot the boot password
How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?
Wine travel Jianghu War: Ctrip is strong, meituan is strong, and Tiktok is fighting
智能合约安全审计公司选型分析和审计报告资源下载---国内篇
[set theory] set concept and relationship (set family | set family examples | multiple sets)
Reptile exercise 02
[set theory] binary relationship (definition field | value field | inverse operation | inverse synthesis operation | restriction | image | single root | single value | nature of synthesis operation)
C primre plus Chapter 10 question 6 inverted array
Arthas watch grabs a field / attribute of the input parameter
Introduction of pointer variables in function parameters
How to use kotlin to improve productivity: kotlin tips
2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical
[NLP]—sparse neural network最新工作简述
FFMpeg filter
Integration of Android high-frequency interview questions (including reference answers)
Web - Information Collection
MySQL field userid comma separated save by userid query
2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
C Primer Plus Chapter 10, question 14 3 × 5 array