leetcode dfs Palindrome Partitioning

Palindrome Partitioning

Total Accepted: 21056 Total
Submissions: 81036My Submissions

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

Return all possible palindrome partitioning of s.

For example, given s = "aab",

Return

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

题意:切割字符串,使每一个子串都是回文

思路:dfs

选择一个切割点时,假设从起点到切割点的子串是回文,那么该切割点是合法的。能够选择

按这个规则dfs枚举就能够了

复杂度:时间O(n)。空间O(log n)

vector<vector<string> >res;

bool is_palindrome(const string &s, int start, int end){
	while(start < end){
		if(s[start] != s[end - 1]) return false;
		++start; --end;
	}
	return true;
}

void dfs(const string &s, int cur, vector<string>& partitions){
	int size = s.size();
	if(cur == size){
		res.push_back(partitions);
	}
	for(int end = cur + 1; end <= size; ++end){
		if(is_palindrome(s, cur, end)){
			partitions.push_back(s.substr(cur, end - cur));
			dfs(s, end, partitions);
			partitions.pop_back();
		}
	}
}

vector<vector<string>> partition(string s) {
	vector<string> tem;
	dfs(s, 0, tem);
	return res;
}
时间: 2024-10-29 19:09:51

leetcode dfs Palindrome Partitioning的相关文章

leetcode之Palindrome Partitioning

方法一:DFS递归,判断每一个是否为回文数1,首先要有一个判断字符串是否是回文的函数.容易实现,字符串从两边同时往中间走,看字符是否相同; 2,深度优先搜索思想对字符串进行遍历.得到结果.例如,s = "abacd"; 需要对"a""ad""aba""abac""abacd"进行深度优先搜索.深度搜索的过程如下:先选"a",发现"a"是回文,则深度

[LeetCode]132.Palindrome Partitioning II

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&qu

【算法】leetcode之 Palindrome Partitioning I&amp;II(转载)

1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有可能的情况. 该问题的难度比较大,很可能第一次遇到没有思路,这很正常.下面我们一点点分析,逐步理清思路.先不考虑所有的情况,针对一个符合条件的划分,每一部分都是一个回文子串,而且各部分的长度不固定.也即每一部分都是原始字符串的一个子串,且满足回文条件.所有的划分都满足上述条件,所以这就启发我们首先判

[C++]LeetCode: 121 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. For example, given s = "aab", Return [ ["aa","b"], ["a","a",&

[LeetCode][Java] 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. For example, given s = "aab", Return [ ["aa","b"], ["a","a",&

【leetcode】Palindrome Partitioning

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. For example, given s = "aab",Return [ ["aa","b"], ["a&qu

【LeetCode】Palindrome Partitioning II

Palindrome Partitioning II  Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome p

Java for LeetCode 132 Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa"

【LeetCode】Palindrome Partitioning II 解题报告

[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&