java - generate longest possible palindrome for a given string -
i've been trying generate longest possible palindrome included in given string, in java. but, ended in errors.
let me provide sample input , output may .
input: this sample string testing
output: ttissaepeassitt
it great if solve me this!!
thank you!!
you use recursive algorithm:
public string findpalindrom(string input){ string palindrom = ""; for(int i=0; i<input.length(); i++){ char c = input.charat(i); // explore string beginning, char char for(int j=input.length()-1; j>i; j--){ // explore string end, char char if(input.charat(j)==c){ // found letter palindrom string newpalindrom = c + findpalindrom(input.substring(i+1, j)) + c; if(newpalindrom.length() > palindrom.length()) palindrom = newpalindrom; } } } if(palindrom.length()==0 && input.length()>0) palindrom += input.charat(0); // manage case palindrom possible single char. return palindrom; }
Comments
Post a Comment