HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Largest palindrome in a string

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
largestpalindromestring

Problem

I am working on a question in which I need to find a largest palindrome given a string. Here is the description.

public class LargestPalindrome {
  public static void main(String[] args) {
    System.out.println("Longest Palindrome 1: " +findLongestPalindrome("xyzracecar"));  
    System.out.println("Longest Palindrome 2: " +findLongestPalindrome("abcexcbaddd"));
    System.out.println("Longest Palindrome 3: " +findLongestPalindrome("cacxx"));
  }

  public static String findLongestPalindrome(String str) {
    String palindrome = ""; // initializing empty string

    for(int i=0; ii; j--) {
       String data = str.substring(i, j);
        if(isPalindrome(data)) {
          if(data.length() > palindrome.length()) {
            palindrome = data;
          }
        }
      }
    }

    return palindrome;
  }

  /**
   * This method is used to check whether a given string is palindrome or not
   * 
   */
  public static boolean isPalindrome(String s) {
    int last = s.length() - 1;

    for(int i=0; i<s.length()/2; i++) {
      if(s.charAt(i) != s.charAt(last)) {
        return false;
      }
      last--;
    }

    return true;  
  }
}


The code is working fine. Can it be optimized anymore?

Solution

Two minor points on the code and one on unit test:

-
Combining if clauses

You can combine both if clauses as such:

if (isPalindrome(current) && current.length() > palindrome.length()) { ... }


-
Enhancing use of for loop

Multiple local variables can be declared in the for loop, so you can include your last variable as such:

private static boolean isPalindrome(String s) {
    for (int i = 0, last = s.length() - 1; i < s.length() / 2; i++, last--) {
        if (s.charAt(i) != s.charAt(last)) {
            return false;
        }
    }
    return true;
}


-
Unit testing

As mentioned in my comment, it will be better to turn your main() code into a series of unit tests to ensure your logic is working correctly. The example below uses TestNG and Hamcrest matchers, and is meant to be a simple showcase for how this can be done. You should consider a variety of tests (e.g. empty Strings, non-palindromic inputs) to also ensure it works for as many cases as possible. That is when you can consider some of the features of TestNG such as parameterized testing.

@Test
public void doTest() {
    assertThat(findLongestPalindrome("xyzracecar"), equalTo("racecar"));
    assertThat(findLongestPalindrome("abcexcbaddd"), equalTo("ddd"));
    assertThat(findLongestPalindrome("cacxx"), equalTo("cac"));
}


Bonus: Oh yeah, is it the largest or longest palindrome you are looking for? Since you are dealing with Strings, my suggestion is to be consistent and stick with permutations of 'longest palindrome'. :)

Code Snippets

if (isPalindrome(current) && current.length() > palindrome.length()) { ... }
private static boolean isPalindrome(String s) {
    for (int i = 0, last = s.length() - 1; i < s.length() / 2; i++, last--) {
        if (s.charAt(i) != s.charAt(last)) {
            return false;
        }
    }
    return true;
}
@Test
public void doTest() {
    assertThat(findLongestPalindrome("xyzracecar"), equalTo("racecar"));
    assertThat(findLongestPalindrome("abcexcbaddd"), equalTo("ddd"));
    assertThat(findLongestPalindrome("cacxx"), equalTo("cac"));
}

Context

StackExchange Code Review Q#88183, answer score: 2

Revisions (0)

No revisions yet.