-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1930-unique-length-3-palindromic-subsequences.js
More file actions
38 lines (35 loc) · 1.1 KB
/
1930-unique-length-3-palindromic-subsequences.js
File metadata and controls
38 lines (35 loc) · 1.1 KB
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
/**
* 1930. Unique Length-3 Palindromic Subsequences
* https://leetcode.com/problems/unique-length-3-palindromic-subsequences/
* Difficulty: Medium
*
* Given a string s, return the number of unique palindromes of length three that are a
* subsequence of s.
*
* Note that even if there are multiple ways to obtain the same subsequence, it is still
* only counted once.
*
* A palindrome is a string that reads the same forwards and backwards.
*
* A subsequence of a string is a new string generated from the original string with some
* characters (can be none) deleted without changing the relative order of the remaining
* characters.
*
* For example, "ace" is a subsequence of "abcde".
*/
/**
* @param {string} s
* @return {number}
*/
var countPalindromicSubsequence = function(s) {
let result = 0;
for (let i = 0; i < 26; ++i) {
const char = String.fromCharCode(i + 97);
const left = s.indexOf(char);
const right = s.lastIndexOf(char);
if (left !== -1 && right !== -1 && left < right) {
result += new Set(s.substring(left + 1, right)).size;
}
}
return result;
};