Python:每日一题 368
今天的题目:给定字符串 s 和字符串数组 words,求 words 中是 s 的子串的字符串个数。
示例:
输入:s = "abcde", words = ["a", "bb", "acd", "ace"]
输出:3
解释:有三个是 s 的子序列的单词:"a","acd",和 "ace" 。
{:10_298:}欢迎大家一起答题!{:10_298:} 本帖最后由 March2615 于 2020-4-4 21:18 编辑
s里面是可以重复的?
比如输入:s=“abcdea”, words = ["cda"]
这时输出是1?
def daily368(s: str, words: list) -> int:
ans = 0
word_list = {}
for word in words:
if word not in word_list.keys():# 需要一个key为26个字母、value为列表储存后续单词的字典
word_list] = []
word_list].append(word)
for ch in s:
if ch in word_list:
ch_word = word_list# ch_word是以ch为开头的单词
del word_list
for word in ch_word:
if word == '':
ans += 1
else:
if word not in word_list.keys():
word_list] = []
word_list].append(word)
return ans
就按照我的想法来了
思路是把words按照首字母分类,首字母在s里面出现后,删除首字母,将剩余的单词重新分类,遍历一次s就可以得到答案 def fun368(s,words):
cnt = 0
for each in words:
t = s[:]
for letter in each:
if letter not in t:
break
else:
t = t.replace(letter,'')
else:
cnt += 1
return cnt 本帖最后由 TJBEST 于 2020-4-4 21:08 编辑
先来一个效率低的
def fun368(s,words):
def isSub(sub):
temp = s
for each in sub:
try:
position = temp.index(each)
temp = temp[(position+1):]
except Exception:
return False
return True
result = 0
for eachword in words:
if isSub(eachword):
result += 1
return result def f368(s,words):
def find_str(s,str1):
index=-1
for i in str1:
index = s.find(i,index+1,len(s))
if index == -1:
return False
else:
return True
count = 0
for j in words:
if find_str(s,j):
count += 1
return count 本帖最后由 ouyunfu 于 2020-4-4 21:47 编辑
import re
def f368(s,words):
count=0
for i in words:
s1 = s[:]
for m in i:
if m not in s1:
break
else: s1=s1.replace(m,'')
else:
a=) for j in i]
if sorted(a)==a:
count+=1
return count @zltzlt 我个人理解 子串的元素顺序得和s的一致吧 比如'asd'的子串不包括'sa'但是包含'ad'? 本帖最后由 kinkon 于 2020-4-5 00:10 编辑
看例子感觉有顺序要求啊
def f368(s, words):
out = 0
for w in words:
left = 0
bg = True
for x in w:
tmp = s.find(x)
if tmp == -1:
bg = False
break
else:
left = tmp + 1
if bg:
out += 1
return out
再来一个,试试哪个快
from collections import Counter
def f368(s, words):
s_cout = Counter(s)
out = 0
for w in words:
w_cout = Counter(w)
if all(s_cout >= w_cout for x in w_cout):
if list((s_cout + w_cout - s_cout).elements()) == list(w):
out += 1
return out def a368(s,words):
s,count = list(s),0
for i in words:
k = 0
for j in s:
if j == list(i):
k += 1
if k == len(list(i)):
count += 1
break
print(count)
a368('abcde',['a','bb','acd','ace']) def fun368(s,words):
count = 0
def issubseq(s, t):
index = 0
for i in s:
index = t.find(i, index)
if index < 0:
return False
index += 1
return True
for j in words:
if issubseq(j,s):
count += 1
else:
continue
return count def f368(s:str,words:list)->int:
count=0
for i in words:
m = 0
for j in s:
if j == i:
m += 1
if m == len(i):
count += 1
break
return count adc是不是acde的子序列 def func368(s,words):
temp = set(s)
dicts = {}
count = 0
for each in temp:
dicts = s.count(each)
for i in range(len(words)):
temp = set(words)
for j in temp:
if words.count(j) > dicts:
count -= 1
break
count += 1
return count fan1993423 发表于 2020-4-4 23:41
adc是不是acde的子序列
应该不是 难度评级:简单
要素分析:字符串 查找
代码:
def solve(s:str,words:list)->int:
res = 0
for word in words:
i = 0
for each in word:
i = s.find(each,i)+1
if not i:
break
else:
res += 1
return res
if __name__ == '__main__':
print('示例 输出:',solve('abcde',['a','bb','acd','ace']))
忐忑.png
不知道超时否。 占楼 def find_sub_str(string, str_list):
str_num = 0
def is_substring(str1, str2):
j = 0
for i in range(len(str2)):
find_position = str1.find(str2, j)
if find_position != -1:
j = find_position + 1
else:
return False
else:
return True
for i in range(len(str_list)):
if is_substring(string, str_list):
str_num += 1
return str_num def f(s,words):
dict1 = {}
for each in s:
if each not in dict1.keys():
dict1 = 0
dict1 += 1
def f1(dict1,s):
for each in s:
if s.count(each) <= dict1:
s.replace('each','')
else:
return False
return True
n = 0
for each in words:
if f1(dict1,each):
n += 1
return n def f(s,w):
for i in w:
a=s.find(i)
if a==-1:
return 0
s=s
return 1
s = "abcde"
words = ["a", "bb", "acd", "ace"]
print(sum(f(s,w) for w in words))
import re
resultnum=0
def just(s,word): #判断word是否是s的子字符串,是返回True,否返回False
length=len(word)
for i in range(length):
rflag=re.search(word,s)
if rflag==None:
return False
t=rflag.span()
index=t
s=s
return True
if __name__=='__main__':
enter=input("请输入检测的字符串,以‘,’隔开:")
s=input("请输入给定的字符串")
words=
for each in words:
if just(s,each)==True:
resultnum+=1
print(each)
print (resultnum)