你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度n ~= 500,000),而 s 是个短字符串(长度 <=100)。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
进阶:时间复杂度
,空间复杂度%5C)
你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度n ~= 500,000),而 s 是个短字符串(长度 <=100)。
共两行,第一行为字符串s, 第二行为字符串t
字符串t的长度 1<=n<=500000
字符串s的长度 1<=m<=100
输出true或者是false,true表示是s是t的子序列,false表示s不是t的子序列
abc ahbgdc
true
axc ahbgdc
false
t = input("请输入子字符串:") s = input("请输入字符串:") start = 0 flag = 0 for i in t: if s.find(i, start) != -1: start = s.find(i) else: print('false') flag = 1 break if not flag: print('true')
s, t = input(), input() begin = 0 # 定义在字符串t中开始查找s字符的起始位置 result = True # 在字符串t中逐个查找s的每个字符 for i in range(len(s)): if s[i] in t[begin:]: # 当找到s[i]时,修改下次查找s[i+1]时t的起始位置 begin = begin + t[begin:].find(s[i]) else: # 如果s[i]没有找到则结束循环 result = False break if not result: print('false') else: print('true')
while True: try: s = input() t = input() #记录结果 tmp = '' #历遍s,按顺序比对t中是否出现 for i in range(len(s)): #历遍t for v, k in enumerate(t): #如果t中查询到s中的字母,将该字母加入tmp,并更新t if s[i] == k: tmp += k #更新t,避免重复查询 t = t[v + 1::] break #比较结果,如果tmp和s相同就是true,反之false if tmp == s: print('true') else: print('false') except: break
target = input() source = input() index = -1 for ch in target[::-1]: index = source.rfind(ch) if index != -1: source = source[0:index] else: break print('false' if index == -1 else 'true')