LeetCode Palindrome Partitioning, Palindrome Partitioning II

来源:互联网 发布:ios9关闭蜂窝移动数据 编辑:程序博客网 时间:2024/05/28 05:14

Palindrome Partitioning

Description:

Given a string, an we need to find out all the palindrome partition, and store them in a List.

Solution:

I think we can split this problem into two steps.

1. find all the palindrome

2. since we need to store all the possible solutions, so simply a searching is enough and necessary

a better way to solve this problem is use the recorded arrays or lists like string[i], records all the possible solutions beginning with character i

import java.util.*;public class Solution {int length;boolean match[][];public ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();public ArrayList<ArrayList<String>> partition(String s) {length = s.length();match = new boolean[length + 2][length + 2];for (int i = 0; i < length; i++)match[i][i] = true;for (int i = 1; i < length; i++)if (s.charAt(i - 1) == s.charAt(i))match[i - 1][i] = match[i][i - 1] = true;int j;for (int l = 1; l <= length; l++) {for (int i = 0; (j = i + l - 1) < length; i++) {if (match[i][j] && i - 1 >= 0 && j + 1 < length&& s.charAt(i - 1) == s.charAt(j + 1)) {match[i - 1][j + 1] = true;}}}dfs(s, 0, "");return list;}void dfs(String s, int index, String next) {// this is where we can add our better solution, like below// if (record[index]!=null)//next * record[index] is what we want => list.add(...) if (index == length) {list.add(new ArrayList<String>(Arrays.asList(next.split(" "))));} else {for (int i = index; i < length; i++) {if (match[index][i]) {if (index != 0)dfs(s, i + 1, next + " " + s.substring(index, i + 1));elsedfs(s, i + 1, s.substring(index, i + 1));}}// to implement the better solution, we need to record[index] here}}public static void main(String[] args) {Solution s = new Solution();String str = "cbbbcc";System.out.println(s.partition(str));}}



Palindrome Partitioning II

Description:
Given a string, and we have to figure out the minimum cut that needed to cut it into palindrome partition.
Solution:
Also in two steps.
1. find all the possible palindrome, also store it in the boolean[][] match
2. use a int[] count to represent that least cut number that needed to cut the substring [i,l). And also be aware that any palindrome partition from i can be written in [i,j] + count[j+1,l), where [i,j] is a palindrome
so count[i] = min( 1 + count[j+1] ), match[i][j] = true

public class Solution {int l;boolean match[][];int count[];int min;public int minCut(String s) {l = s.length();match = new boolean[l][l];count = new int[l + 1];for (int i = 0; i < l; i++)match[i][i] = true;for (int i = 1; i < l; i++)if (s.charAt(i - 1) == s.charAt(i))match[i - 1][i] = true;int j;for (int len = 1; len <= l; len++)for (int i = 0; (j = i + len - 1) < l; i++) {if (match[i][j] && i - 1 >= 0 && j + 1 < l&& s.charAt(i - 1) == s.charAt(j + 1))match[i - 1][j + 1] = true;}for (int i = l - 1; i >= 0; i--) {count[i] = 1 + count[i + 1];for (j = i; j < l; j++) {if (match[i][j])count[i] = Math.min(count[i], count[j + 1] + 1);}}return count[0] - 1;}public static void main(String[] args) {Solution s = new Solution();String st = "efe";System.out.println(s.minCut(st));}}




0 0
原创粉丝点击