Find the Longest Palindrome in a String by Removing Characters

1

Problem: Remove characters from strings to make the longest (character length) palindrome possible. The first line gives an integer value of the number of strings to test. Each subsequent line is a string to test.

Example Input:

3 // Number of lines to test
GGABZXVYCCDBA
ABGBAD
ABC

Example Output:

ABCCBA
ABGBA
B

Shortest code wins.

Connor

Posted 2014-01-25T16:52:29.297

Reputation: 119

Question was closed 2014-01-26T17:30:32.497

4Would A and C also be acceptable answers for the last test? – shiona – 2014-01-25T17:02:06.473

3Please give an objective winning criterion. – Howard – 2014-01-25T17:56:58.663

1From your example I infer that you ask for a complete program which can process multiple strings as input. The problem specification only asks for a single string - can you please clarify. Also does largest means longest/most characters? – Howard – 2014-01-25T17:59:52.893

3Why is this tagged "Java"? – Darren Stone – 2014-01-25T18:19:44.393

seems close enough to be duplicate – shiona – 2014-01-25T19:07:19.347

@Howard Basically the same question. This one asks for the longest one, and that one asks for all of them. You could just take the longest and then you would have an answer to this question. Voted to close as duplicate – Doorknob – 2014-01-25T23:45:14.000

@shiona A and C would also be acceptable answers for the last test. If there is a tie for the largest palindrome, pick any one of them. – Connor – 2014-01-26T15:17:08.047

@DoorknobofSnow this is not a duplicate. This one requires that you *remove characters to make the palindrome. – Connor – 2014-01-26T15:19:31.133

@Connor That one does too. "Subsequence" means any sequence you can get by removing 1 or more characters. – Doorknob – 2014-01-26T15:20:41.450

@DoorknobofSnow Oh. I didn't know my bad. I'm still pretty new to this stuff. – Connor – 2014-01-26T15:23:45.090

Answers

0

Here is my solution in Java (not exactly short hah)...

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Palindrome {

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(new File("src/pal.dat"));

        int q = sc.nextInt();
        sc.nextLine();

        for (int z = 0; z < q; z++) {
            String line = sc.nextLine();

            int max = 0;
            String maxP = "";

            for (int k = 0; k < line.length(); k++) {

                String p = calc(line.substring(k));

                if (p.length() > max) {
                    max = p.length();
                    maxP = p;
                }

            }

            System.out.println("Max Palindrome: " + maxP);

        }

    }

    public static String calc(String s) {
        if (s.length() == 0)
            return "";
        if (s.length() == 1 || (s.length() == 2 && s.charAt(0) != s.charAt(1)))
            return s.charAt(0) + "";
        if (s.length() == 2)
            return s;

        char[] arr = s.toCharArray();

        boolean match = false;
        int i = 0;
        for (i = arr.length - 1; i > 0; i--) {
            if (arr[i] == arr[0]) {
                match = true;
                break;
            }
        }

        int max = 0;
        String maxPalin = "";
        for (int k = 1; k < i; k++) {
            String p = calc(s.substring(k, i));
            if (p.length() > max) {
                max = p.length();
                maxPalin = p;
            }
        }

        if (match)
            return arr[0] + maxPalin + arr[0];
        return maxPalin;

    }

}

Connor

Posted 2014-01-25T16:52:29.297

Reputation: 119