Three Algorithms to check for Palindrome String
So, hey everyone. Let me introduce myself to you all, my name is Sreejit Sengupta(pronounced as Sengupto), and I am currently pursuing my B.Tech in Information Technology. I have an interest in the fields of Web-Development, Open-Source or mainly Tech. Without taking much time, let's move into the topic.
What is a Palindrome String?
Before moving to the algorithm part let’s understand what is Palindrome String. A palindrome is a word, phrase, number, or another sequence of characters that reads the same backward as forward. For example, the word “racecar” is a palindrome because it reads the same backward as forward. The phrase “A man, a plan, a canal, Panama!” is also a palindrome because it reads the same backward as forward, ignoring capitalization, punctuation, and word breaks.
In the context of programming, a palindrome string is a string that is the same when read forward or backward. For example, the string “racecar” is a palindrome string because it is the same when read forward or backward.
Let’s look at the algorithms now!
Reversing the String: In this algorithm, we actually reverse the entire string and then check if the reversed String and the original String are equal or not. First, we create a new empty String and a variable of type 'char' then, we run a 'for' loop from the last index of the string till the index is greater than or equal to zero. Store the characters in the 'char' variable declared above and add it to the new String. Here is the Java code of it -
```java static boolean checkForPalindrome(String str) {
str = str.toLowerCase(); if (isEmpty(str)) /* isEmpty() functions checks if the String is empty or null */ { return true; // Since, an empty string is also Palindrome therefore, returning true. } String newStr = ""; // Creating a new String to store the reverse of the original String. char ch; // To store the characters of the original String. for (int i = str.length() - 1; i >= 0; i--) /* Loop to extract characters from the end. */{ ch = str.charAt(i); newStr += ch; // Adding the characters to get the reverse of the original Strings. } return newStr.equals(str); }
/* FUNCTION TO CHECK FOR EMPTY STRING */
static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
```
- Using two pointers: In this algorithm, we use two pointers, one is placed at the 0th index(start pointer) of the String and the other at the last index(end pointer) of the String. We keep on checking if the characters at the 'start' and the 'end' are same or not, if yes we move the 'start' pointer ahead by one index and the 'end' pointer behind by one index and check again. We keep checking till the start <= end if all the characters are same then the String is Palindrome.
We can either use a 'while' loop or a 'for' loop to solve this. Let's look at both of them.
- USING 'for' LOOP: We use a 'for' loop which starts at '0th index' and goes till half of the string, we need not traverse the entire string, we just need to go till the middle of the string after which it starts repeating. Set the 'start' pointer to the character at the '0th' index and the 'end' pointer to the character at the last index. If they are not the same return false, else return true. Here is the Java code of it -
static boolean isPalindrome(String str) {
if (isEmpty) /* isEmpty() functions checks if the String is empty or null */ {
return true;
}
str = str.toLowerCase();
for (int i = 0; i <= str.length() / 2; i++) /* We need not traverse the entire string, we just need to go till the middle of the string after which it starts repeating. */ {
char start = str.charAt(i); // Start pointer
char end = str.charAt(str.length() - 1 - i); // End pointer
if (start != end) /* If at any point the characters are not same we return false. */ {
return false;
}
}
return true;
}
/* FUNCTION TO CHECK FOR EMPTY STRING */
static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
USING 'while' LOOP: We use 'while' loop which executes till the start index is less than or equal to the end index, here 'start' index is initially set to 0(0th index of the String) and 'end' is initially set to length of the string - 1(last index of the String). We keep on checking whether the character at the 'start' and 'end' index are same or not, if yes then return true, else increment 'start' by one and decrement 'end' by one. Here is the Java code of it -
static boolean Palindrome(String str) { if (isEmpty(str)) /* isEmpty() functions checks if the String is empty or null */ { return true; } str = str.toLowerCase(); int start = 0; // Starting index. int end = str.length() - 1; // End index. while(start <= end) { if (str.charAt(start) != str.charAt(end)) /* If the characters at the start and end index are not same then return false. */ { return false; } else /* Else, move start ahead by one and move end behind by one. */{ start++; end--; } } return true; } /* FUNCTION TO CHECK FOR EMPTY STRING */ static boolean isEmpty(String str) { return str == null || str.length() == 0; }
Using StringBuilder: This one is the easiest of them all. We just use the StringBuilder class already provided by Java. Create the object of the StringBuilder class, then run a 'for' loop from the '0th' index to the last index of the String. Use the '.append()' method to add the characters. Then reverse the String using the '.reverse()' method and check if the two String are the same or not. Here is the Java code of it -
static boolean isPalindrome(String str) { if (isEmpty(str)) { return true; } str = str.toLowerCase(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < str.length(); i++) { builder.append(str.charAt(i)); // Adding characters of the original String to the new String. } return builder.reverse().toString().equals(str); // Reversing the String, then checking if the two Strings are equal or not. } /* FUNCTION TO CHECK FOR EMPTY STRING */ static boolean isEmpty(String str) { return str == null || str.length() == 0; }
Thank you for reading my blog post! I hope that you found the information useful and informative. If you have any questions or comments, please don't hesitate to reach out. I would love to hear from you and continue the conversation. Be sure to check out our other blog posts for more interesting and informative content. Thanks again for stopping by!
Here is the link to my Twitter.