06 分割回文串(leecode 131)

1 问题

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。

返回 s 所有可能的分割方案。

示例:
输入: “aab”
输出:
[
[“aa”,“b”],
[“a”,“a”,“b”]
]

2 解法

将切割问题类比为组合问题:
例如对于字符串abcdef:

组合问题:选取一个a之后,在bcdef中再去选取第二个,选取b之后在cdef中在选取第三个…。
切割问题:切割一个a之后,在bcdef中再去切割第二段,切割b之后在cdef中在切割第三段…。
在这里插入图片描述递归用来纵向遍历,for循环用来横向遍历,切割线(就是图中的红线)切割到字符串的结尾位置,说明找到了一个切割方法。

class Solution {
    
    
public:
    vector<vector<string>> res; //存放结果
    vector<string> path; //存放回文串
    //双指针判断s是否为回文
    bool isPalindrome(const string& s, int begin, int end)
    {
    
    
        for(int i = begin, j = end; i < j; i++, j--)
        {
    
    
            if(s[i] != s[j])
                return false;
        }
        return true;
    }

    void backtracking(string s, int startIndex)
    {
    
    
        //如果起始位置已经大于s的大小,说明已经找到了一组分割方案了
        if(startIndex >= s.size())
        {
    
    
            res.push_back(path);
            return;
        }
        for(int i = startIndex; i < s.size(); i++)
        {
    
    
            //判断是否是回文
            if(isPalindrome(s, startIndex, i))  //是回文子串,则加入path
            {
    
    
                //根据索引找出s中[startIndex, i]的子串
                string s1 = s.substr(startIndex, i - startIndex + 1);
                path.push_back(s1); 
            }
            else    //不是回文,跳过
            {
    
    
                continue;
            }
            //递归
            backtracking(s, i + 1);  //i+1表示已经切割的下次不再切割
            //回溯
            path.pop_back();
        }
    }
    vector<vector<string>> partition(string s) {
    
    
        backtracking(s, 0);
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_42454048/article/details/113859789