Java Palindrome Program
- Using For Loop
- Using StringBuilder
- Using Stream API
What is Palindrome?
A palindrome is a string or number that if you read it from the end to the beginning, it will be the same as reading from the beginning to the end. For example:
- "madam" is a palindrome.
- "racecar" is a palindrome.
- "level" is a palindrome.
Java Program to Check if a String is Palindrome using For Loop
In this program, we will check if a given string is a palindrome or not using a for loop. We will compare the characters from the start and end of the string and check if they are equal. We will follow below steps to check if a string is a palindrome or not:
- Convert the given string to lowercase to ignore the case sensitivity.
- Remove all the special characters and spaces from the string.
- Initialize two pointers, one at the start of the string and the other at the end of the string.
- Compare the characters at the start and end of the string.
- If the characters are not equal, then the string is not a palindrome.
- If all the characters are equal, then the string is a palindrome.
public class PalindromeProgram {
public static void main(String[] args) {
String str = "madam";
boolean isPalindrome = true;
// Convert the string to lowercase
str = str.toLowerCase();
// Remove all special characters and spaces
str = str.replaceAll("[^a-zA-Z0-9]", "");
// Initialize two pointers
int start = 0;
int end = str.length() - 1;
// Compare the characters at the start and end of the string
while (start < end) {
if (str.charAt(start) != str.charAt(end)) {
isPalindrome = false;
break;
}
start++;
end--;
}
if (isPalindrome) {
System.out.println("The given string is a palindrome.");
} else {
System.out.println("The given string is not a palindrome.");
}
}
}
Time Complexity: O(n) - Where n is the length of the string, as we are iterating through the string once.
Space Complexity: O(1) - Only uses a constant amount of extra space
Java Program to Check if a String is Palindrome using StringBuilder
In this program, we will check if a given string is a palindrome or not using StringBuilder. We will reverse the given string using StringBuilder and compare it with the original string. If both the strings are equal, then the given string is a palindrome. We will follow below steps to check if a string is a palindrome or not using StringBuilder:
- Convert the given string to lowercase to ignore the case sensitivity.
- Remove all the special characters and spaces from the string.
- Reverse the given string using StringBuilder.
- Compare the reversed string with the original string.
- If both the strings are equal, then the given string is a palindrome.
public class PalindromeProgram {
public static void main(String[] args) {
String str = "madam";
boolean isPalindrome = isPalindrome(str);
if (isPalindrome) {
System.out.println("The given string is a palindrome.");
} else {
System.out.println("The given string is not a palindrome.");
}
}
public static boolean isPalindrome(String str) {
// Convert the string to lowercase
str = str.toLowerCase();
// Remove all special characters and spaces
str = str.replaceAll("[^a-zA-Z0-9]", "");
// Reverse the given string using StringBuilder
StringBuilder reversedStr = new StringBuilder(str).reverse();
// Compare the reversed string with the original string
return str.equals(reversedStr.toString());
}
}
Time Complexity: O(n) - Where n is the length of the string, as we are iterating through the string once.
Space Complexity: O(n) - Where n is the length of the string, as we are creating a new StringBuilder object to reverse the string.
Java Program to Check if a String is Palindrome using Java Stream API
In this program, we will check if a given string is a palindrome or not using Java Stream API. We will reverse the given string using Java Stream API and compare it with the original string. If both the strings are equal, then the given string is a palindrome. We will follow below steps to check if a string is a palindrome or not using Java Stream API:
- Convert the given string to lowercase to ignore the case sensitivity.
- Remove all the special characters and spaces from the string.
- Reverse the given string using Java Stream API.
- Compare the reversed string with the original string.
- If both the strings are equal, then the given string is a palindrome.
import java.util.stream.Collectors;
public class PalindromeProgram {
public static void main(String[] args) {
String str = "madam";
boolean isPalindrome = isPalindrome(str);
if (isPalindrome) {
System.out.println("The given string is a palindrome.");
} else {
System.out.println("The given string is not a palindrome.");
}
}
public static boolean isPalindrome(String str) {
// Convert the string to lowercase
str = str.toLowerCase();
// Remove all special characters and spaces
str = str.replaceAll("[^a-zA-Z0-9]", "");
// Reverse the given string using Java Stream API
String reversedStr = str.chars()
.mapToObj(c -> String.valueOf((char) c))
.collect(Collectors.joining());
// Compare the reversed string with the original string
return str.equals(reversedStr);
}
}
Time Complexity: O(n) - Where n is the length of the string, as we are iterating through the string once.
Space Complexity: O(n) - Where n is the length of the string, as we are creating a new string using Java Stream API.
- Recommended Links
- Selenium Interview Questions