当前位置:网站首页>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
边栏推荐
- Ffmpeg mix
- RSRS指标择时及大小盘轮动
- [fxcg] market analysis today
- Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
- vulnhub HA: Natraj
- 2022-02-14 (394. String decoding)
- X-ray normal based contour rendering
- Busycal latest Chinese version
- [set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
- Joint set search: merge intervals and ask whether two numbers are in the same set
猜你喜欢
P35-P41 fourth_ context
Preliminary cognition of C language pointer
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
Contents of welder (primary) examination and welder (primary) examination in 2022
2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical
MC Layer Target
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
Solve BP Chinese garbled code
2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
Integration of Android high-frequency interview questions (including reference answers)
随机推荐
sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
Redraw and reflow
重绘和回流
RSRS指标择时及大小盘轮动
7. Integrated learning
Xrandr modify resolution and refresh rate
Kubernetes源码分析(一)
SSM based campus part-time platform for College Students
Bugku CTF daily question baby_ flag. txt
[dynamic programming] subsequence problem
[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
Solve BP Chinese garbled code
解决bp中文乱码
[set theory] set concept and relationship (set family | set family examples | multiple sets)
Reptile exercise 02
[NLP]—sparse neural network最新工作简述
Kingbasees plug-in KDB of Jincang database_ date_ function
[BMZCTF-pwn] 18-RCTF-2017-Recho
Php+mysql registration landing page development complete code
Prefix and (continuously updated)