【Lintcode】136.Palindrome Partitioning

题目:

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

Example

Given s = "aab", return:

[
  ["aa","b"],
  ["a","a","b"]
]

题解:

Solution 1 ()

class Solution {
public:
    vector<vector<string>> partition(string s) {
        if (s.empty()) {
            return {{}};
        }
        vector<vector<string> > res;
        vector<string> v;
        dfs(res, v, s, 0);

        return res;
    }
    void dfs(vector<vector<string> > &res, vector<string> &v, string s, int pos) {
        if (pos >= s.size()) {
            res.push_back(v);
            return;
        }
        string str;
        for (int i = pos; i < s.size(); ++i) {
            str += s[i];
            if (isValid(str)) {
                v.push_back(str);
                dfs(res, v, s, i + 1);
                v.pop_back();
            }
        }
    }
    bool isValid(string str) {
        if (str.empty()) {
            return true;
        }
        int begin = 0;
        int end = str.size() - 1;
        for (; begin <= end; ++begin, --end) {
            if (str[begin] != str[end]) {
                return false;
            }
        }
        return true;
    }
};

Solution 1.2 ()

class Solution {
public:
    bool isPalindrome(string &s, int i, int j){
        while(i < j && s[i] == s[j]){
            i++;
            j--;
        }
        if(i >= j) {
            return true;
        } else {
            return false;
        }
    }
    void helper(vector<vector<string>> & res, vector<string> &cur, string &s, int pos){
        if(pos == s.size()){
            res.push_back(cur);
            return;
        }
        for(int i = pos; i <= s.size() - 1; i++){
            if(isPalindrome(s, pos, i)){
                cur.push_back(s.substr(pos, i - pos + 1));
                helper(res, cur, s, i+1);
                cur.pop_back();
            }
        }
    }
    vector<vector<string>> partition(string s) {
        vector<vector<string>> res;
        vector<string> cur;
        helper(res, cur, s, 0);

        return res;
    }
};
时间: 2024-10-20 02:44:08

【Lintcode】136.Palindrome Partitioning的相关文章

【leetcode】1278. Palindrome Partitioning III

题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindro

【动态规划】POJ3280- Cheapest Palindrome

[题目大意] 给出一个字符串,可以删除或添加一些字符,它们各自会消耗价值.问最少消耗多少价值,可以使得字符串变成回文的. [思路] 事实上删除或添加字符的价值只需要保持较小的那一个.假设当前要将(j,i)转换为回文字符,那么它有以下三种情况: (1)在结尾添加或删除一个和开头一样的字符,f[j][i-1]+cost[s[i]-'a']: (2)在开头添加或删除一个和结尾一样的字符,f[j+1][i]+cost[s[j]-'a']: (3)如果开头和结尾的字符本来就是一样的,就有f[j+1][i-

【LeetCode】9 - Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

【LeetCode】234 - Palindrome Linked List

Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? Hide Tags: Linked List Two Pointers 1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 5 typedef struct ListNode

【Lintcode】074.First Bad Version

题目: The code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the unit tests. Find the first bad version. You can call isBa

【Leetcode】Valid Palindrome

题目链接:https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a ca

LeetCode【125】Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that th

【Lintcode】Median of two Sorted Arrays

class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @return: a double whose format is *.5 or *.0 */ int check(vector<int>& A, vector<int>& B,int k,int m) { int M = A.size(); int N = B.size(); int

【LeetCode】- Valid Palindrome(正确的回文)

[ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. 直译:给你一个字符串, 判定它是否是回文(只统计字母.数字,其他字符请忽略). For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" i