

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Checking if a string can be made palindrome in JavaScript
We are required to write a JavaScript function that takes in a string as the first and the only argument.
The task of our function is to check whether we can make that string a palindrome string by deleting at most one character from the string. If we can do so, the function should return true, false otherwise.
For example −
If the input string is −
const str = 'kjlk';
Then the output should be −
const output = true;
because by deleting 'l' from the string, only 'kjk' will be left which is a palindrome string.
Example
The code for this will be −
const str = 'kjlk'; const isPalindrome = (str = '', start, end) => { while (start < end) { if (str[start] != str[end]) { return false; }; start ++; end --; }; return true; }; const canMakePalindrome = (str = '') => { let left = 0, right = str.length - 1; while (left < right - 1) { if (str[left] !== str[right]) { if (isPalindrome(str, left, right - 1)) { return true; }; if (isPalindrome(str, left + 1, right)) { return true; }; return false; }else { left ++; right --; }; }; return true; } console.log(canMakePalindrome(str));
Output
And the output in the console will be −
true
- Related Questions & Answers
- Checking if change can be provided in JavaScript
- Checking if one string can be achieved from another with single tweak in JavaScript
- Check if a string can be rearranged to form special palindrome in Python
- Check if a two-character string can be made using given words in Python
- Checking for permutation of a palindrome in JavaScript
- Check if characters of a given string can be rearranged to form a palindrome in Python
- Is pizza healthy? If not, can it be made healthy?
- Checking if two arrays can form a sequence - JavaScript
- Can a constructor be made final in Java?
- Checking if a string contains all unique characters using JavaScript
- JavaScript - Find if string is a palindrome (Check for punctuation)
- Find if neat arrangement of cups and shelves can be made in Python
- Find if neat arrangement of cups and shelves can be made in C++
- Check if a string is entirely made of the same substring JavaScript
- How can Indian prisons be made secure?
Advertisements