Palindrome Check
Determine whether a given string is a palindrome using optimal techniques.
๐งฉ Problem Statement
Write a function that checks whether a given string is a palindrome โ a string that reads the same backward as forward.
โ๏ธ Function Signature
public boolean isPalindrome(String s)
๐ Approaches
๐ง 1. Two-Pointer Technique (Most Efficient)
public boolean isPalindrome(String s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s.charAt(left++) != s.charAt(right--)) {
return false;
}
}
return true;
}
- Time Complexity: O(n)
- Space Complexity: O(1)
- โ Best for performance-sensitive applications.
๐ 2. Manual Reverse Comparison
public boolean isPalindrome(String s) {
String reversed = "";
for (int i = s.length() - 1; i >= 0; i--) {
reversed += s.charAt(i);
}
return s.equals(reversed);
}
- Time Complexity: O(nยฒ) due to string concatenation.
- Space Complexity: O(n)
- โ Not recommended for large strings due to performance cost.
๐งฑ 3. Using Stack
import java.util.Stack;
public boolean isPalindrome(String s) {
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
stack.push(c);
}
for (char c : s.toCharArray()) {
if (c != stack.pop()) {
return false;
}
}
return true;
}
- Time Complexity: O(n)
- Space Complexity: O(n)
- ๐งฐ Useful if stack-based structure is required.
๐งฎ 4. Recursive Check
public boolean isPalindrome(String s) {
return isPalHelper(s, 0, s.length() - 1);
}
private boolean isPalHelper(String s, int left, int right) {
if (left >= right) return true;
if (s.charAt(left) != s.charAt(right)) return false;
return isPalHelper(s, left + 1, right - 1);
}
- Time Complexity: O(n)
- Space Complexity: O(n) due to call stack.
- ๐ก Elegant, but not optimal for very large strings.
๐งช Test Cases
public class PalindromeCheckTest {
public static void main(String[] args) {
test("madam", true);
test("racecar", true);
test("apple", false);
test("A", true);
test("", true);
test("level", true);
test("deified", true);
test("abcba", true);
test("abccba", true);
test("abcdef", false);
}
private static void test(String input, boolean expected) {
PalindromeSolver solver = new PalindromeSolver();
boolean result = solver.isPalindrome(input);
System.out.println("Input: " + input);
System.out.println("Output: " + result);
System.out.println("Expected: " + expected);
System.out.println(result == expected ? "โ
Passed" : "โ Failed");
System.out.println("-----------------------------");
}
}
๐ Notes
- Case-sensitive:
"Madam"
โ"madam"
โ consider usingtoLowerCase()
if case-insensitive check is desired. - Ignore non-alphanumeric characters: Use
Character.isLetterOrDigit(c)
andCharacter.toLowerCase(c)
if required. - Best Practice: Use the Two-Pointer Approach for most real-world applications due to its efficiency.
๐งฐ Optional Enhancement (Clean and Normalize)
public boolean isPalindrome(String s) {
s = s.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
int left = 0, right = s.length() - 1;
while (left < right) {
if (s.charAt(left++) != s.charAt(right--)) {
return false;
}
}
return true;
}
- โ Ignores special characters and case.
- ๐ Suitable for checking real-world inputs like
"A man, a plan, a canal: Panama"
.
Related Posts
Java Interview Guide - Part 0: Core Questions (1โ50)
A comprehensive list of essential Java interview questions covering OOP, memory management, exceptions, collections, Streams, JDBC
Indepthdev-Backend-Team
Java Interview Guide - Part 4: Advanced Java Concepts (Q31โQ40)
A comprehensive list of essential Java interview questions covering OOP, memory management, exceptions, collections, and Streams
Indepthdev-Backend-Team
Complete Java Mastery
A complete guide to mastering Java, covering core concepts, collections, multithreading, JDBC, and more โ curated by the InDepthDev Backend Team to help you crack interviews and build real-world expertise.
Indepthdev-Backend-Team