Problem Palindrome Partitioning

Problem Description:

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

Return all possible palindrome partitioning of s.

Solution:

 1     public List<List<String>> partition(String s) {
 2 List<List<String>> l = new LinkedList<List<String>>();
 3         List<String> ll = new LinkedList<String>();
 4
 5         if (s.length() == 1) {
 6             ll.add(s);
 7             l.add(ll);
 8             return l;
 9         }
10         for (int i = 0; i < s.length(); i++)  {
11             String str = s.substring(0, i+1);
12             if (! isPalindrome(str)) {
13                 continue;
14             } else {
15                 List<List<String>> others = partition(s.substring(i+1));
16                 if (others.size() > 0) {
17                     for (List<String> other : others) {
18                         other.add(0, s.substring(0, i+1));
19                         l.add(other);
20                     }
21                 } else {
22                     ll.add(str);
23                     l.add(ll);
24                     return l;
25                 }
26
27             }
28         }
29
30         return l;
31     }
32     public boolean isPalindrome(String s) {
33         return s.equals(new StringBuilder(s).reverse().toString());
34     }

Problem Palindrome Partitioning

时间: 2024-10-16 03:47:59

Problem Palindrome Partitioning的相关文章

leetcode || 132、Palindrome Partitioning II

problem: 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 ["

leetcode ||131、Palindrome Partitioning

problem: 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&qu

[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 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

132. Palindrome Partitioning II (String; DP)

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][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",&

Palindrome Partitioning -- leetcode

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 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 pa

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",