You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中生成可拼接还原原字符串的所有子串列表?

How to Generate All Valid Substring Splits That Reconstruct the Original String

Got it, let's work through this together! You're totally right that recursion is a clean, efficient way to solve this problem—breaking the string into valid, reconstructible substrings step by step fits perfectly with recursive logic. Let's walk through a correct implementation and break down how it works.

Core Recursive Logic

The key idea here is to build valid splits incrementally:

  • Start with the full input string.
  • At each step, take a substring from the start of the remaining string (length 1 up to the full remaining length).
  • Recursively split the leftover part of the string after that substring.
  • When we hit an empty string, we've found a valid end point (our base case) to build complete splits from.

Working Python Implementation

Here's a recursive function that does exactly what you need:

def generate_valid_splits(s):
    # Base case: empty string has exactly one valid split (the empty list)
    if not s:
        return [[]]
    
    all_splits = []
    # Iterate over every possible starting substring length
    for i in range(1, len(s) + 1):
        current_sub = s[:i]
        # Recursively get all valid splits for the remaining part of the string
        remaining_splits = generate_valid_splits(s[i:])
        # Attach the current substring to each of those remaining splits
        for split in remaining_splits:
            all_splits.append([current_sub] + split)
    
    return all_splits

Test It with Your Example

If you run this with "hello", here's a sample of the output you'll get:

print(generate_valid_splits("hello"))
# Output includes valid splits like:
# ["h", "e", "l", "l", "o"],
# ["h", "e", "l", "lo"],
# ["h", "e", "ll", "o"],
# ["h", "el", "l", "o"],
# ["he", "l", "l", "o"],
# ... and all other combinations that reconstruct "hello" when concatenated

Why This Works

  • No invalid splits: We only ever take prefixes of the remaining string, so concatenating the substrings in any result list will always give back the original input.
  • Base case clarity: The empty string returns [[]] because there's exactly one way to split nothing—using nothing. This lets us build up complete splits by adding substrings to the front of each valid split from the remaining string.
  • Efficiency: We don't waste time checking invalid combinations; every recursive call explores a valid path toward reconstructing the original string.

Quick Performance Tip

For very long strings, you could add memoization to cache results of already computed substring splits (to avoid redundant recursive calls). But for most use cases, the basic implementation above is fast and straightforward.

内容的提问来源于stack exchange,提问作者yairHas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:36:52