首页 > 编程语言 >python 串联所有单词的子串 多种解法

python 串联所有单词的子串 多种解法

时间:2024-01-12 10:06:05浏览次数:27  
标签:子串 word python substrings substring length words counts 解法

解法一:使用递归

def find_substrings(s, words):
    if not s or not words:
        return []
    
    word_length = len(words[0])
    num_words = len(words)
    total_length = word_length * num_words
    
    substrings = []
    
    def find_substrings_helper(s, words, current_string):
        nonlocal substrings
        
        if len(current_string) == total_length:
            substrings.append(current_string)
            return
        
        for i in range(len(words)):
            if s.startswith(words[i]):
                find_substrings_helper(s[word_length:], words[:i] + words[i+1:], current_string + words[i])
    
    find_substrings_helper(s, words, "")
    
    return substrings

解法二:使用滑动窗口

from collections import Counter

def find_substrings(s, words):
    if not s or not words:
        return []
    
    word_length = len(words[0])
    num_words = len(words)
    total_length = word_length * num_words
    
    substrings = []
    word_counts = Counter(words)
    
    for i in range(len(s) - total_length + 1):
        substring = s[i:i+total_length]
        substring_counts = Counter([substring[j:j+word_length] for j in range(0, total_length, word_length)])
        
        if substring_counts == word_counts:
            substrings.append(substring)
    
    return substrings

解法三:使用哈希表

from collections import defaultdict

def find_substrings(s, words):
    if not s or not words:
        return []
    
    word_length = len(words[0])
    num_words = len(words)
    total_length = word_length * num_words
    
    substrings = []
    word_counts = defaultdict(lambda: 0)
    
    for word in words:
        word_counts[word] += 1
    
    for i in range(len(s) - total_length + 1):
        substring = s[i:i+total_length]
        substring_counts = defaultdict(lambda: 0)
        
        for j in range(0, total_length, word_length):
            substring_counts[substring[j:j+word_length]] += 1
        
        if substring_counts == word_counts:
            substrings.append(substring)
    
    return substrings

标签:子串,word,python,substrings,substring,length,words,counts,解法
From: https://blog.51cto.com/lzning/9210332

相关文章

  • Python数据可视化操作:使用pygal库绘制直方图、XY线图和饼状图
    pygal是一个功能强大的Python库,用于绘制漂亮且交互性强的数据可视化图表。本文将深入介绍如何使用pygal库绘制直方图、XY线图和饼状图,并通过案例讲解帮助读者更好地掌握这些功能。1.绘制直方图直方图是一种用于表示数据分布的图表,通过柱形的高度展示不同数值的频率。下面是绘制直......
  • Python对Excel表格文件数据按条件加以筛选并生成直方图的方法
      本文介绍基于Python语言,读取Excel表格文件数据,以其中某一列数据的值为标准,对于这一列数据处于指定范围的所有行,再用其他几列数据的数值,加以数据筛选与剔除;同时,对筛选前、后的数据分别绘制若干直方图,并将结果数据导出保存为一个新的Excel表格文件的方法。  首先,我们来明确一......
  • Python中的*args和**kwargs
    定义*args和**kwargs是编程人员约定的变量名字,args是arguments的缩写,表示位置参数;kwargs是keywordarguments的缩写,表示关键字参数。这其实就是Python中可变参数的两种形式,并且*args必须放在**kwargs的前面,因为位置参数在关键字参数的前面*args和**kwargs长度......
  • Python 爬取音频如何处理网络请求和响应
    本文将介绍如何使用Python爬取音频,并详细讲解如何处理网络请求和响应,包括发送请求、接收响应、处理状态码和错误等。同时,还会介绍一些常用的第三方库和技巧,帮助你更好地实现音频爬取。1.发送网络请求在Python中,可以使用requests库发送网络请求。首先,需要安装该库:pipinstallreques......
  • Python 在运维中有哪些常见的应用场景
    Python是一种功能强大且易于学习的编程语言,广泛应用于运维领域。本文将介绍Python在运维中的常见应用场景,包括自动化脚本、日志分析、监控系统、配置管理、网络管理和故障排除等方面。1.自动化脚本Python在运维中最常见的应用场景之一就是编写自动化脚本。通过Python脚本,可以自动化......
  • python里面什么是身份运算符
    Python中的身份运算符是一种用于比较对象是否具有相同的内存地址的运算符。在Python中,身份运算符由is和isnot两个关键字组成。本文将详细介绍Python中的身份运算符及其使用方式。is关键字在Python中,is是一个用于比较两个对象是否具有相同内存地址的关键字。当is运算符用于比较两个......
  • 如何使用Python计算列表中所有数字的平均值
    在Python编程中,经常需要对列表中的数字进行各种数学运算。其中一个常见的任务是计算列表中所有数字的平均值。本文将向您介绍如何使用Python编程语言来实现这个任务。步骤:以下是计算列表中所有数字平均值的步骤:1.定义一个包含数字的列表。2.使用`sum()`函数计算列表中所有数字的总......
  • 如何使用Python从列表中删除指定的元素
    在Python编程中,我们经常需要从列表中删除指定的元素。这可以通过使用内置函数和方法来实现。本文将向您介绍如何使用Python语言中的删除函数和方法来删除列表中的元素。1.定义一个包含元素的列表。2.使用`remove()`函数删除列表中指定的元素。3.使用列表解析删除多个指定的元素。4.......
  • 如何使用Python实现字符串反转
     在Python编程中,我们可能需要将字符串反转。这可以通过使用Python内置函数或自定义函数来实现。本文将向您介绍如何使用Python语言中的方法和技巧来反转字符串。 步骤: 以下是实现字符串反转的步骤: 1.定义一个字符串。 2.使用切片`[::-1]`反转字符串。 3.使用循环迭代,将原始......
  • Python中查找字符串某个字符最常用的方法!
    在Python语言中,查找字符串中某个字符是非常普通且常见的操作之一,那么Python如何查找字符串中某个字符?可以使用的方法有很多种,以下是详细内容介绍。1、使用in关键字在Python中,可以使用in关键字来查找一个字符串是否包含某个字符,具体代码如下:```pythons="Hell......