zltzlt 发表于 2020-4-4 20:08:22

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 20:38:36

本帖最后由 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就可以得到答案

BngThea 发表于 2020-4-4 20:40:51

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 20:41:48

本帖最后由 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

l0stparadise 发表于 2020-4-4 20:47:19

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 20:54:17

本帖最后由 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

TJBEST 发表于 2020-4-4 21:12:12

@zltzlt 我个人理解 子串的元素顺序得和s的一致吧 比如'asd'的子串不包括'sa'但是包含'ad'?

kinkon 发表于 2020-4-4 21:20:57

本帖最后由 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

chen971130 发表于 2020-4-4 21:43:58

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'])

jdzzj 发表于 2020-4-4 22:44:38

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

ouyunfu 发表于 2020-4-4 22:50:06

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

fan1993423 发表于 2020-4-4 23:41:44

adc是不是acde的子序列

whosyourdaddy 发表于 2020-4-5 00:06:25

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

kinkon 发表于 2020-4-5 00:11:49

fan1993423 发表于 2020-4-4 23:41
adc是不是acde的子序列

应该不是

阴阳神万物主 发表于 2020-4-5 07:11:30

难度评级:简单
要素分析:字符串 查找
代码:
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
不知道超时否。

永恒的蓝色梦想 发表于 2020-4-5 08:07:48

占楼

旅途Z 发表于 2020-4-5 10:52:47

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

风魔孤行者 发表于 2020-4-5 11:19:13

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

山岂乎不在高 发表于 2020-4-5 12:26:11

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))

lucky邪神 发表于 2020-4-5 14:26:37

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)
页: [1] 2 3
查看完整版本: Python:每日一题 368