Data Structures and Algorithms (DSA)  

✨ How to Check if a String is a Palindrome in DSA

πŸ” What is a Palindrome?

A palindrome is a string that remains the same when reversed.
Examples:

  • "madam" β†’ Palindrome βœ…

  • "racecar" β†’ Palindrome βœ…

  • "hello" β†’ Not a Palindrome ❌

This concept is widely used in string manipulation problems, interview coding tests, and pattern-based algorithm challenges.

🧠 Approach to Solve Palindrome Problem

1️⃣ Reverse and Compare

  • Reverse the given string.

  • Compare it with the original string.

  • If both are equal β†’ It’s a palindrome.

πŸ”Έ Time Complexity: O(n)
πŸ”Έ Space Complexity: O(n) (for storing reversed string)

2️⃣ Two-Pointer Technique (Efficient)

  • Place one pointer at the start and one at the end of the string.

  • Compare characters at both positions.

  • If all characters match till the middle β†’ Palindrome.

  • If any mismatch β†’ Not a palindrome.

πŸ”Έ Time Complexity: O(n)
πŸ”Έ Space Complexity: O(1) (in-place check, more efficient)

πŸ’» Code Implementations

βœ… C++ Implementation

#include <iostream>
using namespace std;

bool isPalindrome(string str) {
    int left = 0, right = str.length() - 1;
    while (left < right) {
        if (str[left] != str[right]) {
            return false;
        }
        left++;
        right--;
    }
    return true;
}

int main() {
    string s = "racecar";
    if (isPalindrome(s))
        cout << s << " is a palindrome.";
    else
        cout << s << " is not a palindrome.";
    return 0;
}

βœ… Java Implementation

public class PalindromeCheck {
    public static boolean isPalindrome(String str) {
        int left = 0, right = str.length() - 1;
        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    public static void main(String[] args) {
        String s = "madam";
        if (isPalindrome(s))
            System.out.println(s + " is a palindrome.");
        else
            System.out.println(s + " is not a palindrome.");
    }
}

βœ… Python Implementation

def is_palindrome(s: str) -> bool:
    return s == s[::-1]

# Test
s = "level"
if is_palindrome(s):
    print(f"{s} is a palindrome.")
else:
    print(f"{s} is not a palindrome.")

πŸ† Practical Applications of Palindrome Check

  • Data Validation: Checking symmetric patterns in data.

  • Cryptography: Palindromes can appear in encoded/decoded strings.

  • Interview Questions: Common coding problem for beginners.

  • DNA Sequences: Some biological DNA strands form palindrome patterns.

πŸ“Œ Key Takeaways

  • A palindrome reads the same forward and backward.

  • Two main approaches: Reverse & Compare (simple) and Two-Pointer (efficient).

  • Time Complexity: O(n).

  • Frequently asked in DSA interviews and coding challenges.

πŸ‘‰ By mastering this problem, you strengthen your string manipulation skills β€” a foundation for advanced DSA topics like pattern matching, dynamic programming, and hashing problems.