Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Checking if a string can be made palindrome in JavaScript
In JavaScript, determining if a string can be made into a palindrome by removing at most one character is a common algorithmic problem. A palindrome reads the same forwards and backwards, like "racecar" or "level".
Problem Statement
Given a string, we need to check if it can become a palindrome by removing at most one character. The function should return true if possible, false otherwise.
Sample Input: "racecar"
Sample Output: true
Using Two Pointers (Recommended)
The most efficient approach uses two pointers starting from both ends of the string, moving towards the center while comparing characters.
function canBePalindrome(str) {
let left = 0;
let right = str.length - 1;
let removed = false;
while (left < right) {
if (str[left] !== str[right]) {
if (removed) {
return false; // Already removed one character
}
// Try removing either left or right character
if (str[left + 1] === str[right]) {
left++;
} else if (str[left] === str[right - 1]) {
right--;
} else {
return false; // Can't make palindrome by removing one character
}
removed = true;
}
left++;
right--;
}
return true;
}
// Test cases
console.log(canBePalindrome("racecar")); // true
console.log(canBePalindrome("abccdba")); // true
console.log(canBePalindrome("abccba")); // true
console.log(canBePalindrome("abcddba")); // false
console.log(canBePalindrome("abcdefg")); // false
true true true false false
Using Recursion with Helper Function
This approach uses recursion to check substrings when characters don't match at the endpoints.
function canFormPalindrome(str) {
// Helper function to check if a substring is a palindrome
function isPalindrome(left, right) {
while (left < right) {
if (str[left] !== str[right]) {
return false;
}
left++;
right--;
}
return true;
}
// Recursive function to check palindrome possibility
function checkPalindrome(left, right) {
if (left >= right) {
return true; // Base case: single character or empty
}
if (str[left] === str[right]) {
return checkPalindrome(left + 1, right - 1);
}
// Try removing either left or right character
return isPalindrome(left + 1, right) || isPalindrome(left, right - 1);
}
return checkPalindrome(0, str.length - 1);
}
// Test cases
console.log(canFormPalindrome("abcba")); // true
console.log(canFormPalindrome("abbca")); // true
console.log(canFormPalindrome("abcd")); // false
true true false
Using Dynamic Programming
Dynamic programming creates a 2D table to store results for all possible substrings, avoiding redundant calculations.
function canFormPalindromeDP(str) {
const n = str.length;
// Create 2D DP table
const dp = Array(n).fill(false).map(() => Array(n).fill(false));
// Single characters are always palindromes
for (let i = 0; i < n; i++) {
dp[i][i] = true;
}
// Fill table for substrings of increasing length
for (let len = 2; len <= n; len++) {
for (let i = 0; i < n - len + 1; i++) {
const j = i + len - 1;
if (str[i] === str[j]) {
dp[i][j] = dp[i + 1][j - 1];
} else {
// Try removing either character
dp[i][j] = dp[i + 1][j] || dp[i][j - 1];
}
}
}
return dp[0][n - 1];
}
// Test case
console.log(canFormPalindromeDP("abca")); // true
console.log(canFormPalindromeDP("abcd")); // false
true false
Comparison of Methods
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Two Pointers | O(n) | O(1) | Most efficient, recommended |
| Recursion | O(n) | O(n) | Clear logic, moderate efficiency |
| Dynamic Programming | O(n²) | O(n²) | Complex cases with multiple modifications |
Conclusion
The two-pointer approach is the most efficient solution with O(n) time and O(1) space complexity. It's the recommended method for checking if a string can be made palindrome by removing at most one character.
