-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCountPalindromicSubsequences.js
40 lines (37 loc) · 1.12 KB
/
CountPalindromicSubsequences.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @author Anirudh Sharma
*
* Given a string s, find number of palindromic subsequence (need not necessarily be distinct)
* which could be formed from the string.
*/
const countPalindromicSubsequences = (s) => {
// Count of all the palindromic subsequences
let count = 0;
// Loop through the string
for (let i = 0; i < s.length; i++) {
// For odd length string, expand from s[i]
count += expandFromMiddle(s, i, i);
// For even length string, expand from s[i]
// and s[i+1]
count += expandFromMiddle(s, i, i + 1);
}
return count;
};
const expandFromMiddle = (s, left, right) => {
let count = 0;
// Check for the equality
while (left >= 0 && right < s.length && s.charAt(left) === s.charAt(right)) {
// Increment the count
count++;
// Expand from both sides
left--;
right++;
}
return count;
};
const main = () => {
console.log(countPalindromicSubsequences("abcd"));
console.log(countPalindromicSubsequences("aab"));
console.log(countPalindromicSubsequences("aaaaaaaaaaaaaaaaaaaaaaaaaaa"));
};
main();