Given a string. Find the longest palindrome subsequence. The string contains an only lowercase letter. Here, I am creating the array with the length of 26 and keeping track of all lowercase letters (i.e how many times they occur in the string). Finally, I count the number of even letters and if one is odd then (odd-1) is added to it.
public int count(String input1){
int res = 0;
int temp = 0;
char chr [] = input1.toCharArray();
int arr[] = new int[26];
for(int i=0;i<chr.length;i++){
arr[chr[i] - 97]++;
}
for(int val:arr){
if(val%2 != 0) temp++;
if(val%2 ==0){
res=res+val;
}
else{
res = res+(val-1);
}
}
if(temp != 0) res++;
return res;
}
}
it's showing undesired output