HomeDSA PRACTICEPalindrome program in PHP

Palindrome program in PHP

Got it πŸ‘ You want a Palindrome program in PHP but explained with DSA (Data Structures & Algorithms) concept.
I’ll give you:

  1. Definition
  2. Algorithm (step by step)
  3. Example
  4. PHP Code Implementation

πŸ”Ή 1. Definition

A palindrome is a string/number that reads the same forwards and backwards.
Examples:

  • "madam", "121", "racecar" βœ… Palindrome
  • "hello", "1234" ❌ Not Palindrome

πŸ”Ή 2. Algorithm (DSA Concept)

We’ll use two-pointer technique (common DSA approach):

  1. Take input string str.
  2. Initialize two pointers:
    • left = 0 (start of string)
    • right = strlen(str) - 1 (end of string)
  3. While left < right:
    • Compare str[left] and str[right]
    • If not equal β†’ Not a palindrome β†’ return false
    • Else move pointers: left++, right--
  4. If loop ends β†’ It’s a palindrome

⏱ Time Complexity: O(n)
πŸ“¦ Space Complexity: O(1)


πŸ”Ή 3. Example

Input: "madam"

  • Compare m (left) with m (right) β†’ βœ…
  • Compare a with a β†’ βœ…
  • Compare d with d β†’ βœ…
    β†’ Palindrome βœ…

Input: "hello"

  • Compare h with o β†’ ❌ Not palindrome

πŸ”Ή 4. PHP Implementation

<?php
function isPalindrome($str) {
    $left = 0;
    $right = strlen($str) - 1;

    while ($left < $right) {
        if ($str[$left] !== $str[$right]) {
            return false; // not palindrome
        }
        $left++;
        $right--;
    }
    return true; // palindrome
}

// Test cases
$words = ["madam", "121", "hello", "racecar"];

foreach ($words as $word) {
    if (isPalindrome($word)) {
        echo "$word is a Palindrome βœ…<br>";
    } else {
        echo "$word is NOT a Palindrome ❌<br>";
    }
}
?>

now solve the Palindrome problem in PHP using Stack (DSA approach).


πŸ”Ή 1. Why Stack?

  • Stack works on LIFO (Last In First Out).
  • If we push all characters of a string into a stack and then pop them, we get the reverse of the string.
  • If the reversed string == original string β†’ It’s a palindrome.

πŸ”Ή 2. Algorithm (Stack-based)

  1. Take input string str.
  2. Initialize an empty stack (array in PHP).
  3. Push every character of the string into the stack.
  4. Pop all characters from stack β†’ This gives reversed string.
  5. Compare reversed string with original:
    • If equal β†’ Palindrome βœ…
    • Else β†’ Not Palindrome ❌

⏱ Time Complexity: O(n)
πŸ“¦ Space Complexity: O(n) (since we store reversed string in stack)


πŸ”Ή 3. PHP Implementation (Stack Approach)

<?php
function isPalindromeStack($str) {
    $stack = [];

    // Push all characters into stack
    for ($i = 0; $i < strlen($str); $i++) {
        array_push($stack, $str[$i]);
    }

    // Pop characters to form reversed string
    $reversed = "";
    while (!empty($stack)) {
        $reversed .= array_pop($stack);
    }

    // Compare original with reversed
    return $str === $reversed;
}

// Test cases
$words = ["madam", "121", "hello", "racecar"];

foreach ($words as $word) {
    if (isPalindromeStack($word)) {
        echo "$word is a Palindrome βœ… (Stack method)<br>";
    } else {
        echo "$word is NOT a Palindrome ❌ (Stack method)<br>";
    }
}
?>

πŸ”Ή 4. Comparison of Approaches

MethodTime ComplexitySpace ComplexityConcept Used
Two-PointerO(n)O(1)Pointers (efficient)
Stack (LIFO)O(n)O(n)Data Structure Stack

πŸ‘‰ The two-pointer method is more efficient.
πŸ‘‰ The stack method is more intuitive for learning DSA concepts.

Share:Β 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *