原文:
给定一个字符串 s,分区 s,使得分区的每个字符串都是回文。返回 s 的所有可能的回文分区。
示例:
input : s = "bcc"
output : [["b", "c", "c"], ["b", "cc"]]
input : s = "geeks"
output : [["g", "e", "e", "k", "s"],
["g", "ee", "k", "s"]]
我们必须列出所有可能的分区,这样我们就会朝着递归的方向思考。当我们在索引 i 上时,我们递增地检查从 i 开始的所有子串是否是回文。如果找到,我们递归地解决剩余字符串的问题,并将其添加到我们的pg电子试玩链接的解决方案中。
以下是pg电子试玩链接的解决方案-
- 我们将维护一个用于存储所有可能分区的二维向量和一个用于存储当前分区的临时向量,字符串的新起始索引用于检查分区,因为我们已经在此索引之前检查过分区。
- 现在继续在字符串上迭代,检查它是否是回文。
- 如果它是回文,那么在当前分区向量中添加这个字符串。如果不是字符串的结尾,则在这个新字符串上递归。再次返回后,将当前分区向量更改为旧的分区向量,因为它在递归步骤中可能已经更改。
- 如果我们在迭代的时候到达了字符串的末尾,那么我们的临时向量中就有了分区,所以我们将把它添加到结果中。
要检查它是否是回文,通过取两个指针来迭代字符串。初始化第一个字符串开始,另一个字符串结束。如果两个字符相同,增加第一个,减少最后一个指针,并继续迭代,直到第一个小于最后一个。
c
// c program to print all palindromic partitions
// of a given string.
#include
using namespace std;
// returns true if str is palindrome, else false
bool checkpalindrome(string str)
{
int len = str.length();
len--;
for (int i=0; i > partitions)
{
for (int i = 0; i < partitions.size(); i)
{
for(int j = 0; j < partitions[i].size(); j)
cout << partitions[i][j] << " ";
cout << endl;
}
return;
}
// goes through all indexes and recursively add remaining
// partitions if current string is palindrome.
void addstrings(vector > &v, string &s,
vector &temp, int index)
{
int len = s.length();
string str;
vector current = temp;
if (index == 0)
temp.clear();
for (int i = index; i < len; i)
{
str = str s[i];
if (checkpalindrome(str))
{
temp.push_back(str);
if (i 1 < len)
addstrings(v,s,temp,i 1);
else
v.push_back(temp);
temp = current;
}
}
return;
}
// generates all palindromic partitions of 's' and
// stores the result in 'v'.
void partition(string s, vector >&v)
{
vector temp;
addstrings(v, s, temp, 0);
printsolution(v);
return;
}
// driver code
int main()
{
string s = "geeks";
vector > partitions;
partition(s, partitions);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to print all palindromic partitions
// of a given string.
import java.util.arraylist;
public class gfg
{
// returns true if str is palindrome, else false
static boolean checkpalindrome(string str)
{
int len = str.length();
len--;
for (int i=0; i>
partitions)
{
for(arraylist i: partitions)
{
for(string j: i)
{
system.out.print(j " ");
}
system.out.println();
}
}
// goes through all indexes and recursively add remaining
// partitions if current string is palindrome.
static arraylist> addstrings(arraylist<
arraylist> v, string s, arraylist temp,
int index)
{
int len = s.length();
string str = "";
arraylist current = new arraylist<>(temp);
if (index == 0)
temp.clear();
// iterate over the string
for (int i = index; i < len; i)
{
str = str s.charat(i);
// check whether the substring is
// palindromic or not
if (checkpalindrome(str))
{
// if palindrome add it to temp list
temp.add(str);
if (i 1 < len)
{
// recurr to get all the palindromic
// partitions for the substrings
v = addstrings(v,s,temp,i 1);
}
else
{
// if end of the string is reached
// add temp to v
v.add(temp);
}
// temp is reinitialize with the
// current i.
temp = new arraylist<>(current);
}
}
return v;
}
// generates all palindromic partitions of 's' and
// stores the result in 'v'.
static void partition(string s, arraylist> v)
{
// temporary arraylist to store each
// palindromic string
arraylist temp = new arraylist<>();
// calling addstring method it adds all
// the palindromic partitions to v
v = addstrings(v, s, temp, 0);
// printing the solution
printsolution(v);
}
// driver code
public static void main(string args[])
{
string s = "geeks";
arraylist> partitions = new
arraylist<>();
partition(s, partitions);
}
}
// this code is contributed by sumit ghosh
python 3
# python3 program to print all palindromic
# partitions of a given string.
def checkpalindrome(string):
# returns true if str is palindrome,
# else false
length = len(string)
length -= 1
for i in range(length):
if string[i] != string[length]:
return false
length -= 1
return true
def printsolution(partitions):
for i in range(len(partitions)):
for j in range(len(partitions[i])):
print(partitions[i][j], end = " ")
print()
def addstrings(v, s, temp, index):
# goes through all indexes and
# recursively add remaining partitions
# if current string is palindrome.
length = len(s)
string = ""
current = temp[:]
if index == 0:
temp = []
for i in range(index, length):
string = s[i]
if checkpalindrome(string):
temp.append(string)
if i 1 < length:
addstrings(v, s, temp[:], i 1)
else:
v.append(temp)
temp = current
def partition(s, v):
# generates all palindromic partitions
# of 's' and stores the result in 'v'.
temp = []
addstrings(v, s, temp[:], 0)
printsolution(v)
# driver code
if __name__ == "__main__":
s = "geeks"
partitions = []
partition(s, partitions)
# this code is contributed by
# vibhu4agarwal
c
// c# program to print all palindromic partitions
// of a given string.
using system;
using system.collections.generic;
class gfg
{
// returns true if str is palindrome, else false
static bool checkpalindrome(string str)
{
int len = str.length;
len--;
for (int i = 0; i < len; i )
{
if (str[i] != str[len])
return false;
len--;
}
return true;
}
// prints the partition list
static void printsolution(list>
partitions)
{
foreach(list i in partitions)
{
foreach(string j in i)
{
console.write(j " ");
}
console.writeline();
}
}
// goes through all indexes and recursively add remaining
// partitions if current string is palindrome.
static list> addstrings(list<
list> v, string s, list temp,
int index)
{
int len = s.length;
string str = "";
list current = new list(temp);
if (index == 0)
temp.clear();
// iterate over the string
for (int i = index; i < len; i)
{
str = str s[i];
// check whether the substring is
// palindromic or not
if (checkpalindrome(str))
{
// if palindrome add it to temp list
temp.add(str);
if (i 1 < len)
{
// recurr to get all the palindromic
// partitions for the substrings
v = addstrings(v,s,temp,i 1);
}
else
{
// if end of the string is reached
// add temp to v
v.add(temp);
}
// temp is reinitialize with the
// current i.
temp = new list(current);
}
}
return v;
}
// generates all palindromic partitions of 's' and
// stores the result in 'v'.
static void partition(string s, list> v)
{
// temporary list to store each
// palindromic string
list temp = new list();
// calling addstring method it adds all
// the palindromic partitions to v
v = addstrings(v, s, temp, 0);
// printing the solution
printsolution(v);
}
// driver code
public static void main(string []args)
{
string s = "geeks";
list> partitions = new
list>();
partition(s, partitions);
}
}
// this code is contributed by princiraj1992
output :
g e e k s
g ee k s
相关文章:
本文由安舒·戈亚尔供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 contribute@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。
如果您发现任何不正确的地方,或者您想分享更多关于上面讨论的主题的信息,请写评论
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处