Skip to content

Commit 9f02777

Browse files
committedMar 1, 2025
Add solution #1930
1 parent e7b0c0d commit 9f02777

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@
623623
1920|[Build Array from Permutation](./1920-build-array-from-permutation.js)|Easy|
624624
1926|[Nearest Exit from Entrance in Maze](./1926-nearest-exit-from-entrance-in-maze.js)|Medium|
625625
1929|[Concatenation of Array](./1929-concatenation-of-array.js)|Easy|
626+
1930|[Unique Length-3 Palindromic Subsequences](./1930-unique-length-3-palindromic-subsequences.js)|Medium|
626627
1935|[Maximum Number of Words You Can Type](./1935-maximum-number-of-words-you-can-type.js)|Easy|
627628
1980|[Find Unique Binary String](./1980-find-unique-binary-string.js)|Medium|
628629
1985|[Find the Kth Largest Integer in the Array](./1985-find-the-kth-largest-integer-in-the-array.js)|Medium|
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 1930. Unique Length-3 Palindromic Subsequences
3+
* https://leetcode.com/problems/unique-length-3-palindromic-subsequences/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s, return the number of unique palindromes of length three that are a
7+
* subsequence of s.
8+
*
9+
* Note that even if there are multiple ways to obtain the same subsequence, it is still
10+
* only counted once.
11+
*
12+
* A palindrome is a string that reads the same forwards and backwards.
13+
*
14+
* A subsequence of a string is a new string generated from the original string with some
15+
* characters (can be none) deleted without changing the relative order of the remaining
16+
* characters.
17+
*
18+
* For example, "ace" is a subsequence of "abcde".
19+
*/
20+
21+
/**
22+
* @param {string} s
23+
* @return {number}
24+
*/
25+
var countPalindromicSubsequence = function(s) {
26+
let result = 0;
27+
28+
for (let i = 0; i < 26; ++i) {
29+
const char = String.fromCharCode(i + 97);
30+
const left = s.indexOf(char);
31+
const right = s.lastIndexOf(char);
32+
if (left !== -1 && right !== -1 && left < right) {
33+
result += new Set(s.substring(left + 1, right)).size;
34+
}
35+
}
36+
37+
return result;
38+
};

0 commit comments

Comments
 (0)