[Leetcode] [Java] Palindrome Partitioning

Source: Internet
Author: User

Title:

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

Return all possible palindrome partitioning of s.

For example, given s = "aab" ,
Return

  [    ["AA", "B"],    ["A", "a", "B"]  ]
Test Instructions:

Given a string s, divide the string so that each partition is a palindrome string.

Returns the division of all palindrome strings for S.

For example, given s = "aab" ,

Return

  [    ["AA", "B"],    ["A", "a", "B"]  ]

Algorithm Analysis:

Backtracking recursion, thinking more clear, look at the code better understanding.

AC Code:

<span style= "Font-family:microsoft yahei;font-size:12px;" >public class Solution {public arraylist<arraylist<string>> partition (String s) {arraylist< arraylist<string>> list = new arraylist<arraylist<string>> (); arraylist<string> tmp = new arraylist<string> (); Getrs (list, TMP, s); return list;} public void Getrs (arraylist<arraylist<string>> list,arraylist<string> tmp,string s) {if (S.length () ==0| | S==null) {List.add (new arraylist<string> (TMP)); return;} else{for (int i = 1;i<=s.length (); i++) {if (Ispalindrome (s.substring (0, I))//This substring starts at the specified beginindex until the index endIndex- 1 characters {tmp.add (s.substring (0, I)); Getrs (list, TMP, s.substring (i));//The substring starts at the character at the specified index until the end of the string. Tmp.remove (Tmp.size ()-1);}}}} Determines whether a string is a palindrome public boolean ispalindrome (string s) {if (s==null| | S.length () ==0) return true;for (int i = 0,J = S.length () -1;i<j;i++,j--) {if (S.charat (i)!=s.charat (j)) return false;} return true;} }</span>

Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode] [Java] Palindrome Partitioning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.