
- 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
Counting the number of palindromes that can be constructed from a string in JavaScript
We are required to write a JavaScript function that takes in a string of characters as the first argument, say str, and a number, say num, as the second argument.
The function should count the number of palindrome strings all exactly of length num can be constructed from the given string str. The function should then finally return the count.
For example −
If the input string and the number is −
const str = 'ij'; const num = 4;
Then the output should be −
const output = 4;
because those four possible palindrome strings are −
'iiii', 'jjjj', 'ijji', 'jiij'
Approach:
We will first count the number of unique letters in the given string using a hash set. If the length of the palindrome is an odd number, the middle character can have u choices where u is the count of unique characters in the string.
When num is even, we will have the following possibilities −
power(u, num/2)
And when num is odd, we need to multiply this number by u since we have u choices for that position.
Example
Following is the code −
const str = 'ij'; const num = 4; const findValidPalindromes = (str = '', num = 1) => { const set = new Set(); for(let i = 0; i < str.length; i++){ const el = str[i]; set.add(el); }; const u = set.size; if(num & 1){ return Math.pow(u, num/2) * u; }else{ return Math.pow(u, num/2); }; }; console.log(findValidPalindromes(str, num));
Output
Following is the console output −
4
- Related Articles
- Count occurrences of a string that can be constructed from another given string in C++
- Program to count number of palindromes of size k can be formed from the given string characters in Python
- Counting the number of redundant characters in a string - JavaScript
- Counting number of vowels in a string with JavaScript
- Counting substrings of a string that contains only one distinct letter in JavaScript
- Number of letters in the counting JavaScript
- Program to check a string can be split into three palindromes or not in Python
- Program to count number of unique palindromes we can make using string characters in Python
- Program to find possible number of palindromes we can make by trimming string in Python
- Counting divisors of a number using JavaScript
- Counting number of words in a sentence in JavaScript
- Counting prime numbers from 2 upto the number n JavaScript
- Can the string be segmented in JavaScript
- Program to count number of palindromes after minimum number of split of the string in C++
- Counting the number of 1s upto n in JavaScript
