生成字符串中子串的组合

mik*_*kel 5 c# string recursion combinations substring

我正在尝试为给定的单词生成所有可能的音节组合.识别什么是音节的过程在这里是不相关的,但它产生的所有组合都给我一个问题.我认为这可能是我想的几行递归(尽管其他任何方式都很好),但是我无法让它工作.有人可以帮忙吗?

    // how to test a syllable, just for the purpose of this example
    bool IsSyllable(string possibleSyllable) 
    {
        return Regex.IsMatch(possibleSyllable, "^(mis|und|un|der|er|stand)$");
    }

    List<string> BreakIntoSyllables(string word)
    {
       // the code here is what I'm trying to write 
       // if 'word' is "misunderstand" , I'd like this to return
       //  => {"mis","und","er","stand"},{ "mis","un","der","stand"}
       // and for any other combinations to be not included
    }
Run Code Online (Sandbox Code Playgroud)

Eni*_*ity 4

尝试从这个开始:

var word = "misunderstand";

Func<string, bool> isSyllable =
    t => Regex.IsMatch(t, "^(mis|und|un|der|er|stand)$");

var query =
    from i in Enumerable.Range(0, word.Length)
    from l in Enumerable.Range(1, word.Length - i)
    let part = word.Substring(i, l)
    where isSyllable(part)
    select part;
Run Code Online (Sandbox Code Playgroud)

这将返回:

误解结果

这至少在一开始有帮助吗?


编辑:我进一步思考了这个问题并提出了这几个查询:

Func<string, IEnumerable<string[]>> splitter = null;
splitter =
    t =>
        from n in Enumerable.Range(1, t.Length - 1)
        let s = t.Substring(0, n)
        let e = t.Substring(n)
        from g in (new [] { new [] { e } }).Concat(splitter(e))
        select (new [] { s }).Concat(g).ToArray();

var query =
    from split in (new [] { new [] { word } }).Concat(splitter(word))
    where split.All(part => isSyllable(part))
    select split;
Run Code Online (Sandbox Code Playgroud)

现在query返回这个:

误解-结果2

让我知道现在是否解决了。