LeetCode Palindrome Partitioning, Palindrome Partitioning II

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));
					else
						dfs(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值