Please have a look at how I read in test cases

Hi
i am trying to solve the longest palindromic sequence, and my code runs fine with the few test cases I have in IDE, but I think I am not reading the test cases of this site correctly. Would you have a look?

public class CodeChef {
    public int longestPalindString(String string) {
        if (string == null || string.length() == 0) return 0;
        int currentCharacter = 1, low, high, longestLength = 1, currentLength = 1;
        if (string.length() == 2 && string.charAt(currentCharacter - 1) == string.charAt(currentCharacter)) return 2;
        while (currentCharacter < string.length() - 1) {
            if (currentCharacter % 2 == 1) {
                low = currentCharacter - 1;
                high = currentCharacter + 1;
                while (low >= 0 && high <= string.length() - 1) {
                    if (string.charAt(low) == string.charAt(high)) {
                        // currentLength = high - low + 1;
                        currentLength = Math.max(currentLength, high - low + 1);
                        low--;
                        high++;
                    } else break;
                }
            } else {
                low = currentCharacter - 1;
                high = currentCharacter;
                while (low >= 0 && high <= string.length() - 1) {
                    if (string.charAt(low) == string.charAt(high)) {
                        currentLength = high - low + 1;
                        low--;
                        high++;
                    } else break;
                }
            }
            if (longestLength < currentLength) longestLength = currentLength;
            currentCharacter++;
        }
        return longestLength;
    }

    public static void main(String[] args) {
        int testCaseNum;
        String testString;
        CodeChef codeChef = new CodeChef();
        for (int i = 0; i < args.length; i++) {
            testCaseNum =  Character.getNumericValue(args[0].charAt(0));
            for (int j = 0; j < testCaseNum; j++) {
                testString = args[1+j];
                codeChef.longestPalindString(testString);
            }
        }
    }
    /*
        String test1 = "";
        String test2 = "a";
        String test3 = "aa";
        String test4 = "bab";
        String test5 = "abbaaab";
        LongestPalindromicString longestPalindromicString = new LongestPalindromicString();
        System.out.println("Test 1 - Expect 0, result: " + longestPalindromicString.longestPalindString(test1));
        System.out.println("Test 2 - Expect 1, result: " + longestPalindromicString.longestPalindString(test2));
        System.out.println("Test 3 - Expect 2, result: " + longestPalindromicString.longestPalindString(test3));
        System.out.println("Test 4 - Expect 3, result: " + longestPalindromicString.longestPalindString(test4));
        System.out.println("Test 5 - Expect 4, result: " + longestPalindromicString.longestPalindString(test5));
        */
}
1 Like

Hi @monazzam - i dont know Java well enough but you can refer to this code to see how the inputs are being taken - CodeChef: Practical coding for everyone - caveat → this is also giving a WA

thats confusing

My answer was wrong. I looked up the solution. Thanks for the reply.