
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Find the Length of the Longest possible palindrome string JavaScript
Given a string s which consists of lowercase or uppercase letters, we are required to return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome here.
For example −
If the input string is −
const str = "abccccdd";
then the output should be 7,
because, one longest palindrome that can be built is "dccaccd", whose length is 7.
Example
const str = "abccccdd"; const longestPalindrome = (str) => { const set = new Set(); let count = 0; for (const char of str) { if (set.has(char)) { count += 2; set.delete(char); } else { set.add(char); } } return count + (set.size > 0 ? 1 : 0); }; console.log(longestPalindrome(str));
Output
And the output in the console will be −
7
- Related Articles
- Length of the longest possible consecutive sequence of numbers in JavaScript
- Length of longest string chain in JavaScript
- Program to find length of longest chunked palindrome decomposition in Python
- Program to find length of longest possible stick in Python?
- Find and return the longest length of set in JavaScript
- Finding the length of longest vowel substring in a string using JavaScript
- Longest possible string built from two strings in JavaScript
- Find longest length number in a string in C++
- Find longest palindrome formed by removing or shuffling chars from string in Python
- Find longest palindrome formed by removing or shuffling chars from string in C++
- Find the Nth Even Length Palindrome using C++
- Find length of longest subsequence of one string which is substring of another string in C++
- How to find the length of the longest substring from the given string without repeating the characters using C#?
- Finding the character with longest consecutive repetitions in a string and its length using JavaScript
- Find longest string in array (excluding spaces) JavaScript

Advertisements