Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Experiment 2.

Student Name: Rupam UID: 21BCS5624


Branch: BE-CSE Section/Group:907 -B
Semester: 6 Date of Performance:01-03-2024
Subject Name: Advance Programming lab
Subject Code:21CSP-251

1. Aim:
● To Solve Count and Say
● To Solve 1 bit and 2 bits characters
2. Objective:

● The count-and-say sequence is a sequence of digit strings defined by the


recursive formula: countAndSay(1) = "1" countAndSay(n) is the way you would
"say" the digit string from countAndSay(n-1), which is then converted into
a different digit string.

● We have two special characters: The first character can be represented by


one bit 0. The second character can be represented by two bits (10 or 11).

3. Algo. /Approach and output:

class Solution {
public:
string countAndSay(int n) {
if(n == 1) return "1";
string s = countAndSay(n-1);
int i = 0, j = 0;
string r;
while(i < s.size()) {
j = i+1;
while(j < s.size() && s[j] == s[i])
++j;
string ct = to_string(j-i);
r += ct + s[i];
i = j;
}
return r;
}
};

class Solution {
public:
bool isOneBitCharacter(vector<int>& bits)
{
int n = bits.size();
if(n == 1)
return true;
int i = 0;
while(i <= n - 2)
{
if(bits[i] == 0)
i++;
else
i = i + 2;
}
if(i <= n-1)
return true;
else
return false;

}
};

You might also like