DEV Community

Sangeeth raj
Sangeeth raj

Posted on

Palindrome

A palindrome, such as madam or racecar, is a word, number, phrase, or other sequence of letters that reads the same backward as forward. The following are some examples of when palindrome is used:

  • Get the number from user
  • Hold the number in temporary variable
  • Reverse the number
  • Compare the temporary number with reversed number
  • If both numbers are same, print palindrome number
  • Else print not palindrome number

Method 01

Explanation :
Here we will be using a simple logic to check palindrome in easy steps. STEP 01 get the string and reverse it, then STEP 02 compare the original String and Reverse string values if they are equal return true(palindrome) else return false(palindrome).

Original String and Reverse string

Code :

public class Palindrome {

    public static String palindromeCheck(String n) {
        /* return variable */
        String ans = "";        

        /* variable to store reverse string */
        String temp = "";

        /* for loop to reverse the string */
        for (int i = n.length() - 1; i >= 0; i--) {
            /* concatenate character in string */
            temp += n.charAt(i);
        }

        /* compares two strings return true is they are equal */
        if (n.equals(temp)) {
            ans = "Palindrome";
        } else {
            ans = "Not Palindrome";
        }

        /* Return the result*/
        return ans;
    }

    public static void main(String[] args) {
        /* declare the string to check palindrome */
        String name = "dfhh";

        /* call the palindromeCheck method */
        System.out.println(palindromeCheck(name));

    }

}
Enter fullscreen mode Exit fullscreen mode

Output :
Not Palindrome

Steps :

  1. Define and declare a string variable in the main method.
  2. Call the user defined method in the print statement.
  3. Define a variable for return value.
  4. Define a temp variable to store reverse string.
  5. Using For loop to reserve the string.
  6. Use charAt() function to retrieve value and concatenate string value the in temp variable.
  7. Use equals() function to compares two strings return true is they are equal.
  8. Return the result.

Method 02

Explanation :
This a complex code but efficient, we will be comparing the character in the front value(i) of the string to rear value(j) of the string as mentioned in the below image. We will iterate the loop half the time of the length of the string. In the iteration when comparison is done front value will be incremented and rear value will be decremented. Example if the length of the string is 6 the loop will be iterate for 3 time, using the equality operator "==" we will compare primitives then if all the condition is true then it will true return.

Character comparision

Code :

public class Palindrome {

    public static boolean palindromeCheck(String n) {
        /* Declare variable for front */
        int i = 0;

        /* Declare variable for rear */
        int j = n.length() - 1;

        /* Declare variable for middle */
        int k = (i + j) / 2;

        /* loop to iterate half the length of the string */
        for (int l = 0; l <= k; l++) {

            /* condition to compare primitives */
            if (n.charAt(i) == n.charAt(j)) {

                /* increase value the front */
                i++;

                /* decrease the value of rear */
                j--;
            } else {

                /* Return the result */
                return false;
            }
        }

        /* Return the result */
        return true;
    }

    public static void main(String[] args) {
        /* declare the string to check palindrome */
        String name = "MADAM";

        /* call the palindromeCheck method */
        System.out.println(palindromeCheck(name));

    }

}
Enter fullscreen mode Exit fullscreen mode

Output :
true

Steps :

  1. Define and declare a string variable in the main method.
  2. Call the user defined method in the print statement.
  3. Define a variable for front value.
  4. Define a variable for rear value.
  5. Define a variable for middle value.
  6. Run for loop half the value of string.
  7. Compare primitives of string from front value and rear value using "==" operator.
  8. Increase front value and decrease rear value in iteration.
  9. Return the result.

Special Note: Palindrome is commonly asked in interview, method 01 is an easy implementation and method 02 is an efficient implementation. Hope this will help someone in future!! I welcome your feedback and solutions-oriented suggestions, thank you for your time.

Happy Coding❤️❤️❤️

Top comments (0)