Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Palindrome detector in Java

farenda 2017-05-05 0

Palindrome is a sequence of chars which reads the same backward and forward. Detecting palindrome is a common job interview task therefore we’ll show how to implement that.

Fast Palindrome Detector

This fast algorithm goes through string from both ends and expects them to be the same. As soon as they start to differ we know that this is not a palindrome.

package com.farenda.tutorials.algorithms.strings;

public class PalindromeDetector {

    public boolean isPalindrome(String s) {
        for (int i = 0, j = s.length()-1; i <= j; ++i, --j) {
            // front char != back char
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}

Notice that we compare the chars only to the middle of the string, because after that they start to repeat – remember, palindrome from both sides is the same. ;-)

Slow and easy Palindrome Detector

This implementation is presumably slower than the previous one, but is much more readable and requires no explanation:

public boolean isSlowPalindrome(String s) {
    return new StringBuilder(s).reverse().toString().equals(s);
}

Unit tests

To check that it really works, let’s write some JUnit tests:

package com.farenda.tutorials.algorithms.strings;

import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class PalindromeDetectorTest {

    private PalindromeDetector detector = new PalindromeDetector();

    @Test
    public void shouldFindPalindromes() {
        assertTrue(detector.isPalindrome(""));
        assertTrue(detector.isPalindrome("a"));
        assertTrue(detector.isPalindrome("aba"));
        assertTrue(detector.isPalindrome("abba"));
        assertTrue(detector.isPalindrome("madamimadam"));

        assertTrue(detector.isSlowPalindrome("madamimadam"));
    }

    @Test
    public void shouldDetectNotPalindromes() {
        assertFalse(detector.isPalindrome("ab"));
        assertFalse(detector.isPalindrome("abab"));
        assertFalse(detector.isPalindrome("random string"));

        assertFalse(detector.isSlowPalindrome("random string"));
    }
}

References:

  • How to reverse String in Java
  • JUnit Tutorial
Share with the World!
Categories Algorithms Tags algorithms, interview, strings
Previous: Shuffle Algorithm in Java
Next: Initialize Spring bean after construction

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored