Got it π You want a Palindrome program in PHP but explained with DSA (Data Structures & Algorithms) concept.
Iβll give you:
- Definition
- Algorithm (step by step)
- Example
- 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):
- Take input string
str. - Initialize two pointers:
left = 0(start of string)right = strlen(str) - 1(end of string)
- While
left < right:- Compare
str[left]andstr[right] - If not equal β Not a palindrome β return false
- Else move pointers:
left++,right--
- Compare
- If loop ends β Itβs a palindrome
β± Time Complexity: O(n)
π¦ Space Complexity: O(1)
πΉ 3. Example
Input: "madam"
- Compare
m(left) withm(right) β β - Compare
awithaβ β - Compare
dwithdβ β
β Palindrome β
Input: "hello"
- Compare
hwithoβ β 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)
- Take input string
str. - Initialize an empty stack (array in PHP).
- Push every character of the string into the stack.
- Pop all characters from stack β This gives reversed string.
- 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
| Method | Time Complexity | Space Complexity | Concept Used |
|---|---|---|---|
| Two-Pointer | O(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.
