File tree 2 files changed +39
-0
lines changed 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 623
623
1920|[ Build Array from Permutation] ( ./1920-build-array-from-permutation.js ) |Easy|
624
624
1926|[ Nearest Exit from Entrance in Maze] ( ./1926-nearest-exit-from-entrance-in-maze.js ) |Medium|
625
625
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|
626
627
1935|[ Maximum Number of Words You Can Type] ( ./1935-maximum-number-of-words-you-can-type.js ) |Easy|
627
628
1980|[ Find Unique Binary String] ( ./1980-find-unique-binary-string.js ) |Medium|
628
629
1985|[ Find the Kth Largest Integer in the Array] ( ./1985-find-the-kth-largest-integer-in-the-array.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments