Introduction

The Palindrome String problem involves checking whether a string reads the same forward and backward.

Given a string s, the task is to determine:

  • whether the string is a palindrome
  • and return true or false

A palindrome remains unchanged after reversal.

This problem helps in understanding:

  • string traversal
  • comparison logic
  • Two Pointer Technique
  • symmetry checking

Example

Input:s = "madam"

Output:
True

Explanation:
Forward:
madam

Backward:
madam
Both are same.
Input:
s = "hello"

Output:
False
Explanation:
Forward:
hello

Backward:
olleh
Both are different.

Constraints

 1 <= s.length <= 10^5 s contains lowercase English letters

Approach 1 : Brute Force

Explanation

The simplest way to solve this problem is:

  1. Reverse the string
  2. Compare reversed string with original string

If both are equal:

  • string is palindrome

Otherwise:

  • not palindrome

Steps

  1. Reverse the string.
  2. Compare reversed string with original.
  3. If equal:
    • return true
  4. Otherwise:
    • return false

Dry Run

Input:s = "madam"
Reverse string:
"madam"
Compare:
madam == madam
Condition true

Final Result:
True

Brute Force Code

Complexity Analysis

Time Complexity: O(n)Explanation:
String reversal and comparison are performed.
Space Complexity: O(n)
Explanation:
Extra reversed string is used.

Approach 2 : Optimized Solution (Two Pointer Technique)

Explanation

The optimized solution uses the Two Pointer Technique.

The idea is:

  1. Place:
    • left pointer at beginning
    • right pointer at end
  2. Compare characters.
  3. If characters differ:
    • not palindrome
  4. Otherwise:
    • move pointers inward

This avoids creating an extra reversed string.

Steps

  1. Initialize:
    • left = 0
    • right = n - 1
  2. Compare characters.
  3. If mismatch found:
    • return false
  4. Move pointers inward.
  5. Return true if traversal completes.

Dry Run

Input:s = "madam"

Initially:
left = 0 → m
right = 4 → m
Characters match
Move pointers left = 1 → a
right = 3 → a
Characters match
Move pointers
left = 2 → d
right = 2 → d
Traversal completed
Final Result:
True

Optimized Code

Complexity Analysis

Time Complexity: O(n)Explanation:
Each character is checked once.

Space Complexity: O(1)
Explanation: No extra data structures are used.

Edge Cases

  1. String contains one character
  2. String contains even length
  3. String contains odd length
  4. String contains special characters
  5. String is not palindrome

Why This Problem is Important

This problem helps in understanding:

  1. Two Pointer Technique
  2. String comparison
  3. Symmetry checking
  4. Traversal logic
  5. Efficient validation

It is one of the most common string interview problems.

Real-World Applications

Palindrome checking concepts are used in:

  1. Text validation systems
  2. DNA sequence analysis
  3. Pattern matching
  4. Data verification
  5. Language processing systems

Common Mistakes

  1. Incorrect pointer updates
  2. Wrong comparison logic
  3. Forgetting case sensitivity
  4. Loop boundary mistakes

Interview Tips

Interviewers often expect:

  1. Two Pointer optimization
  2. Efficient comparison logic
  3. O(1) space solution

Always explain why only half traversal is sufficient.

Related Questions

  1. Reverse String
  2. Valid Palindrome
  3. Longest Palindrome
  4. Anagram Check
  5. Reverse Words

Final Takeaway

The Palindrome String problem is a fundamental string problem that teaches comparison and Two Pointer traversal techniques. Understanding this problem builds a strong foundation for advanced string matching and validation interview problems.