File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 40
40
34|[ Find First and Last Position of Element in Sorted Array] ( ./0034-find-first-and-last-position-of-element-in-sorted-array.js ) |Medium|
41
41
35|[ Search Insert Position] ( ./0035-search-insert-position.js ) |Easy|
42
42
36|[ Valid Sudoku] ( ./0036-valid-sudoku.js ) |Medium|
43
+ 38|[ Count and Say] ( ./0038-count-and-say.js ) |Medium|
43
44
39|[ Combination Sum] ( ./0039-combination-sum.js ) |Medium|
44
45
40|[ Combination Sum II] ( ./0040-combination-sum-ii.js ) |Medium|
45
46
41|[ First Missing Positive] ( ./0041-first-missing-positive.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 38. Count and Say
3
+ * https://leetcode.com/problems/count-and-say/
4
+ * Difficulty: Medium
5
+ *
6
+ * The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
7
+ * - countAndSay(1) = "1"
8
+ * - countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is
9
+ * then converted into a different digit string.
10
+ *
11
+ * To determine how you "say" a digit string, split it into the minimal number of substrings such
12
+ * that each substring contains exactly one unique digit. Then for each substring, say the number
13
+ * of digits, then say the digit. Finally, concatenate every said digit.
14
+ *
15
+ * For example, the saying and conversion for digit string "3322251":
16
+ * Given a positive integer n, return the nth term of the count-and-say sequence.
17
+ */
18
+
19
+ /**
20
+ * @param {number } n
21
+ * @return {string }
22
+ */
23
+ var countAndSay = function ( n ) {
24
+ let result = '1' ;
25
+
26
+ for ( let i = 1 ; i < n ; i ++ ) {
27
+ result = result . replace ( / ( ( \d ) \2* ) / g, '$1—' )
28
+ . split ( '—' )
29
+ . map ( s => s ? `${ s . length } ${ s [ 0 ] } ` : '' )
30
+ . join ( '' ) ;
31
+ }
32
+
33
+ return result ;
34
+ } ;
35
+
You can’t perform that action at this time.
0 commit comments