Split palindrome
Title description: Give you a string
s
, please divides
into some substrings, so that each substring is palindrome string . Return all possible segmentation schemes fors
palindrome string is a string that is read both forward and backward.
Please refer to LeetCode official website for example description.
Source: LeetCode
Link: https://leetcode-cn.com/problems/palindrome-partitioning/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Solution one: recursive method
First, handle two special cases. If the string is null, return an empty result set directly; if the length of the string is 1, there is only one split case, and return this case directly.
When the length of the string is greater than 1, it is processed in a recursive manner, in which a method isHuiwen is used to determine whether the string is a palindrome. The recursive process is as follows:
- Judging from the first character of the string, the parameters include the previously partitioned palindrome list, the current position, and the current substring to be judged;
- First judge if the last character of the string has been processed, if the current partition string is a palindrome, add the current partition string to partitions, and then add it to the result set, otherwise, return directly;
Otherwise, first determine whether the current partition string is a palindrome string, there are two possibilities:
- If it is, add the current partition string to partitions, and start the recursive judgment with the next character as the new partition string;
- If it is not, add the next character to the current partition string and judge it recursively.
Finally, the result set is returned.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class LeetCode_131 {
// 结果集
private static List<List<String>> result = new ArrayList<>();
public static List<List<String>> partition(String s) {
// 如果字符串为null,直接返回空结果集
if (s == null) {
return new ArrayList<>();
}
// 如果字符串只有一个字符,只可能有一个结果,直接返回
if (s.length() == 1) {
List<String> partition = new ArrayList<>();
partition.add(s);
result.add(partition);
return result;
}
partition(s, 0, new ArrayList<>(), s.substring(0, 1));
return result;
}
/**
* 递归方法
*
* @param s 原始字符串
* @param pos 当前位置
* @param partitions 当前位置之前已经被分割的回文串
* @param curPartition 当前分区字符串
*/
private static void partition(String s, int pos, List<String> partitions, String curPartition) {
// 已经处理到字符串的最后一个字符
if (pos == s.length() - 1) {
if (isHuiwen(curPartition)) {
// 如果当前分区字符串是回文串,则将当前分区字符串添加到partitions,然后将之添加到结果集中
List<String> newPartitions = new ArrayList<>(Arrays.asList(new String[partitions.size()]));
Collections.copy(newPartitions, partitions);
newPartitions.add(curPartition);
result.add(newPartitions);
}
return;
}
// 如果当前分区字符串是回文串,则将当前分区字符串添加到partitions,然后递归判断下一个字符
if (isHuiwen(curPartition)) {
List<String> newPartitions = new ArrayList<>(Arrays.asList(new String[partitions.size()]));
Collections.copy(newPartitions, partitions);
newPartitions.add(curPartition);
partition(s, pos + 1, newPartitions, s.substring(pos + 1, pos + 2));
}
// 递归处理下一个字符串
partition(s, pos + 1, partitions, curPartition + s.substring(pos + 1, pos + 2));
}
/**
* 判断字符串是否是回文串
*
* @param str 字符串
* @return
*/
private static boolean isHuiwen(String str) {
if (str == null || str.length() < 2) {
return true;
}
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
for (List<String> string : partition("aab")) {
System.out.println(string);
}
}
}
[Daily Message] 161aacbaa442bd Abandon the small ambition of the
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。