给一个字符串,你可以选择在一个字符或两个相邻字符之后拆分字符串,使字符串由仅一个字符或两个字符组成,输出所有可能的结果
样例
给一个字符串"123"
返回[["1","2","3"],["12","3"],["1","23"]]
最近博客一直打不开,粘上来,睡觉。。
class Solution {
public:
/*
* @param : a string to be split
* @return: all possible split string array
*/
vector<vector<string>> splitString(string s) {
// write your code here
vector<string> temp;
vector<vector<string>> result;
int length=s.length();
splitStringHelper(0,s,temp,result);
return result;
}
void splitStringHelper(int start,string& s,vector<string> temp,vector<vector<string>>& result)
{
if(start>=s.length())
{
result.push_back(temp);
return;
}
vector<string> tmp=temp;
tmp.push_back(s.substr(start,1));
splitStringHelper(start+1,s,tmp,result);
if(start+2<=s.length()){
//temp.erase(temp.end());
temp.push_back(s.substr(start,2));
splitStringHelper(start+2,s,temp,result);
}
}
};