#123 - Count-and-say the Sequence
+500 pointsContributed by: Noor Alam Shuvo
Can you count and say what characters the string '11121'
has?
It has three '1'
characters, followed by one '2'
, and finally one '1'
. Or to put it another way, it has 3 1s, 1 2s and 1 1, which we could write as the string '311211'
. For this problem, we define this transformation as "counting and saying". Counting and saying '11121'
is '311211'
.
For this problem, you have to generate a sequence given an Integer N
. We define base case for N = 1
as '1'
. For N > 1
, count and say the sequence generated by N - 1.
Examples:
countAndSay(1) == '1' // base case countAndSay(2) == '11' // previous sequence has one 1. countAndSay(3) == '21' // previous sequence has two 1s countAndSay(4) == '1211' // previous sequence has one 2, one 1 countAndSay(5) == '111221' // ... and so on countAndSay(6) == '312211' countAndSay(7) == '13112221'
Write a function that takes N (sequence number) as a parameter and outputs the Nth sequence. 1<= N <= 20
Discuss this problem on the ApexSandbox.io Trailblazer Community Group
Loading...
offline