Longest Subsequence palindrome java implementation

package com.test.dev;

public class LongestSubSequencePalindrome {

/**
* @param args
*/
public static void main(String[] args) {
String str = "tatestseatat"; // Any test String
char modChars[] = str.toCharArray();
int length = modChars.length;
int maxSubSeqLen = getMaxSubSeq(modChars,0,length-1);
System.out.println((maxSubSeqLen));
}

private static int getMaxSubSeq(char[] modChars,int j, int i) {
if(i-j <= 1 || (i-j == 2 && modChars[i] != modChars[j])){
return 1;
}
if(i-j == 2 && modChars[i] == modChars[i-1]){
return 2;
}
if(modChars[i] == modChars[j]){
return (2 + getMaxSubSeq(modChars,j+1,i-1));
}
return Math.max(getMaxSubSeq(modChars,j,i-1), getMaxSubSeq(modChars,j+1,i));
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *