Split string into words

后端 未结 4 1108
予麋鹿
予麋鹿 2021-02-03 13:27

I am looking for the most efficient algorithm to form all possible combinations of words from a string. For example:

Input String: forevercarrot

Output:

foreve         


        
4条回答
  •  孤独总比滥情好
    2021-02-03 14:12

    A psuedocode implementation, exploiting the fact that every part of the string needs to be a word, we can't skip anything. We work forward from the start of the string until the first bit is a word, and then generate all possible combinations of the rest of the string. Once we've done that, we keep going along until we find any other possibilities for the first word, and so on.

    allPossibleWords(string s, int startPosition) {
        list ret
        for i in startPosition..s'length
            if isWord(s[startPosition, i])
                ret += s[startPostion, i] * allPossibleWords(s, i)
        return ret    
    }
    

    The bugbear in this code is that you'll end up repeating calculations - in your example, you'll end up having to calculate allPossibleWords("carrot") twice - once in ["forever", allPossibleWords["carrot"]] and once in ["for", "ever", allPossibleWords["carrot"]]. So memoizing this is something to consider.

提交回复
热议问题