
- 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 all possible palindromic subsequence within a string in JavaScript
Palindrome Sequence:
A string sequence is known as palindrome sequence if it reads the same from front and the back. For instance, 'aba', 'madam, 'did' are all valid palindrome sequences.
We are required to write a JavaScript function that takes in a string as the first and the only argument. The string taken as input is guaranteed to consist of only 'a', 'b', 'c' and 'd'. Our function should count and return the number of all contiguous or noncontiguous palindrome subsequences that appear in the string.
For example −
If the input string is −
const str = 'bccb';
Then the output should be −
const output = 6;
because the palindrome strings here are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'
Example
The code for this will be −
const str = 'bccb'; const countPalindromes = (str = '') => { let base = 1000000007; const dp = Array(str.length).fill([]); for (let l = 1; l <= str.length; l ++) { for (let i = 0; i + l - 1 < str.length; i ++) { let j = i + l - 1; if (l === 1) { dp[i][j] = 1; continue; } if (l === 2) { dp[i][j] = 2; continue; } if (str[i] === str[j]) { let left = i + 1, right = j - 1; while (left <= right && str[left] != str[i]) { left ++; } while (left <= right && str[right] != str[i]) { right --; } if (left > right) { dp[i][j] = dp[i + 1][j - 1] * 2 + 2; } else if (left === right) { dp[i][j] = dp[i + 1][j - 1] * 2 + 1; } else { dp[i][j] = dp[i + 1][j - 1] * 2 - dp[left + 1][right - 1]; } } else { dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1]; } dp[i][j] = dp[i][j] < 0? dp[i][j] + base : dp[i][j] % base; } } return dp[0][str.length - 1]; }; console.log(countPalindromes(str));
Output
And the output in the console will be −
6
- Related Articles
- Count all Palindromic Subsequence in a given String in C++
- Counting possible APs within an array in JavaScript
- Longest Palindromic Subsequence
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Longest Palindromic Subsequence in C++
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Print all palindromic partitions of a string in C++
- Creating all possible unique permutations of a string in JavaScript
- Java Program for Longest Palindromic Subsequence
- JavaScript function that generates all possible combinations of a string
- Sorting alphabets within a string in JavaScript
- Performing shifts within a string in JavaScript
- Counting prime numbers that reduce to 1 within a range using JavaScript
- Counting number of vowels in a string with JavaScript
- Find all distinct palindromic sub-strings of a given String in Python

Advertisements