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

边栏推荐
- AWS VPC
- Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
- 2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
- Learning practice: comprehensive application of cycle and branch structure (I)
- Prefix and (continuously updated)
- Kingbasees plug-in KDB of Jincang database_ exists_ expand
- RSRS index timing and large and small disc rotation
- Joint set search: merge intervals and ask whether two numbers are in the same set
- When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
- Small program animation realizes the running lantern and animation object
猜你喜欢

vulnhub HA: Natraj
![[free completion] development of course guidance platform (source code +lunwen)](/img/14/7c1c822bda050a805fa7fc25b802a4.jpg)
[free completion] development of course guidance platform (source code +lunwen)

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

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

C language series - Section 3 - functions

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

Preliminary cognition of C language pointer

解决bp中文乱码

有道云笔记

Busycal latest Chinese version
随机推荐
Small program animation realizes the running lantern and animation object
Kubernetes source code analysis (I)
智能合约安全审计公司选型分析和审计报告资源下载---国内篇
After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed
[fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
x Problem B
[free completion] development of course guidance platform (source code +lunwen)
RSRS指标择时及大小盘轮动
金仓数据库KingbaseES 插件kdb_database_link
Reptile exercise 03
Arthas watch grabs a field / attribute of the input parameter
JVM原理简介
Jincang KFS data bidirectional synchronization scenario deployment
多板块轮动策略编写技巧----策略编写学习教材
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
使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
Asp access teaching management system design finished product