-1

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

Alan Sereb
  • 2,358
  • 2
  • 17
  • 31
  • 1
    Show your "undesired output". – cameron1024 Aug 01 '19 at 16:17
  • 1
    I'm not sure algorithm you described is able to find a palindrome subsequence. You are counting how many times a letter encountered, but what about subsequences and palindromes? – M. Prokhorov Aug 01 '19 at 16:26
  • lets suppose I have `"aaaabbccaa"`, so count `a` is 6, `b` is 2 and `c` is 2; all even, result is sum of all (or count of even letters), that is, 10 which is the wrong answer, the longest subsquence is `"aaaa"`, so i think your expectation is wrong (program is doing what is expected) – user85421 Aug 01 '19 at 16:59
  • note: just counting characters cannot work, the sequence is important here. The text I gave as example has the same counts as `"aaabccbaaa"` but here the longest palindrome subsequence has length 10 (or are you searching the longest possible being allowed to reorder the letters? then the *subsequence* part is misleading) - ***Edit*** given algorithm and code is returning size of longest palindrome using the given letters **in any order**, that is, the sequence of letters can be modified – user85421 Aug 01 '19 at 17:08
  • @CarlosHeuberger are you sure that the longest subsequence of "aaaabbccaa" is "aaaa"? – Praveen Kumar Aug 01 '19 at 17:46
  • Obviousky it is not the "longest subsequence" - that would always be the whole string, no calculation needed to know that length. But question asked about longest palindrome subsequence...anyway you are right, it is not the longest palindrome **subsequence** (in Mathematical sense) - I was confusing with **substring**, a continuous subsequence. So the longest subsequence palindrome of `"aaaabbccaa"` must be `"aaaaaa"` or `"aabbaa"` or `"aaccaa"` all length 6, still not the sum of all even chracters (10) If you know a longer one, please post it, so we know what you are searching. – user85421 Aug 01 '19 at 19:12
  • and what is it? – user85421 Aug 02 '19 at 18:17

1 Answers1

1

It's much easier to understand where is the problem, just split activities in separate methods. E.g. isPalindrome() could be in separate method:

public static int findLongestPalindrome(String str) {
    int max = 1;

    for (int i = 0; i < str.length(); i++) {
        for (int j = str.length() - 1; j > i; j--) {
            if (!isPalindrome(str, i, j))
                continue;
            max = Math.max(max, j - i + 1);
            break;
        }
    }

    return max;
}

private static boolean isPalindrome(String str, int i, int j) {
    while (i < j)
        if (str.charAt(i++) != str.charAt(j--))
            return false;
    return true;
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35