python_lintcode_680. Split string

Source: Internet
Author: User
680. Split String Topic

To a string, you can choose to split the string after one character or two adjacent characters so that the string consists of just one character or two characters, outputting all possible results

Examples

Give a string "123"
back [[[1], "2", "3"],["12", "3"],["1", "23"]]
train of ThoughtThis problem uses backtracking method + depth preference similar to intersection there are two kinds of 1 and 2, and then you can go all the way through, the end is index = = Len (s) Here is to select a character or two characters, so first select a character, until the end point to reach the last string (index = Len (s)), copy the result St to the RES that holds the result, then return to the front (-1) to see if there are other paths, then select two characters until the end point reaches the last string (index = Len (s)), and the result St is copied to the res that holds the result. Then return to the front (-2) to see if there are other paths, so all the paths are traversed and all the results are returned. One thing python needs to keep in mind is that the list of reference issues, if you just let the end of the result St put in the result set res, the res will change with St, so you need to use the result of the copy.deepcopy (ST) Copy list in import copy, see details
http://blog.csdn.net/xiongxu3381/article/details/78970060 Code
import copy class Solution: "" "@param:: A string to being split @return: all possible Split string Array "" "Def __init__ (self): self.res =[] def splitstring (self, s): # write you R code here if s = = "": return [[]] st = [] #st来存放结果 Self.dfs (0,s,st) r
            Eturn (self.res) def DFS (self,k,s,st): If K==len (s): Self.res.append (Copy.deepcopy (ST))
            Return St.append (S[k:1+k]) Self.dfs (k+1,s,st) St.pop () if (k+2) <=len (s): St.append (S[k:2+k]) Self.dfs (k+2,s,st) st.pop () 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.