Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
I wrote a logic to return palindromic decompositions of a string, I am having hard time in driving the time complexity of this . Its recursive call inside for loop
Logic used is to iterate over each substring starting with first character, then once we find palindrome , we check the next substring starting from the remaining portition. We od this recursively
Can anyone suggest best possible way to drive this in such cases
public class Solution {
public List<List<String>> partition(String s) {
List<List<String>> result = new ArrayList<List<String>>();
List<String> palindromePartition = new ArrayList<String>();
int start=0;
decompose(s,0,palindromePartition,result);
return result;
}
private void decompose(String input,int startIndex,List<String> palindromePartition,List<List<String>> result) {
if(startIndex==input.length())
{
ArrayList<String> partitionResult = new ArrayList<String>(palindromePartition);
result.add(partitionResult);
return;
}
for(int i=startIndex+1;i<=input.length();i++){
if(isPalindrome(input.substring(startIndex,i))){
palindromePartition.add(input.substring(startIndex,i));
decompose(input,i,palindromePartition,result);
palindromePartition.remove(palindromePartition.size()-1);
}
}
}
private boolean isPalindrome(String input){
int left=0;
int right=input.length()-1;
while(right>left){
if(input.charAt(left)!=input.charAt(right))
return false;
left++;
right--;
}
return true;
}