Count and say problem | Love Babbar DSA Sheet | Leetcode | Amazon | Google🔥

preview_player
Показать описание
#competitiveprogramming #dsasheet #interviewpreparation

In this video I have solved the problem of the sheet i.e. Count and say problem.
Complete Explaination with code.

Hope you like it. Comment if you have any doubt

LIKE | SHARE | SUBSCRIBE
Рекомендации по теме
Комментарии
Автор

When I read the question specifics on leetcode, I just said, what Rubbish this question. But I then realised after seeing this video that the question is still more rubbish and I just misunderstood it the last time. Anyways nice explanation to the problem itself man

shubhamdixit
Автор

the best solutions videos on yt. no nonsense, to the point short sweet. thanks men, you guys deserve more subs!!

black_jack_meghav
Автор

public String countAndSay(int n) {
String s="1";
for(int i=2;i<=n;i++){
s=countAndAdd(s);
}
return s;

}
public String countAndAdd(String s)
{
StringBuilder str=new StringBuilder();
char c=s.charAt(0);
int count=1;

for(int i=1;i<s.length();i++)
{
if(s.charAt(i)==c) count++;

else{
str.append(count);
str.append(c);
c=s.charAt(i);
count=1;
}
}
str.append(count);
str.append(c);

return str.toString();
}

debangshudey
Автор

Python solution :
def count_and_say(n):
if n == 1:
return "1"
s = count_and_say(n - 1)
res = ""
counter = 0

for i in range(0, len(s)):
counter += 1
if i == len(s) - 1 or s[i] != s[i + 1]:
res = res + str(counter) + s[i]
counter = 0

return res

karankakkar
Автор

My code using
string solve(int n){
if(n==1)return "1";
string p=solve(n-1);
string ans="";
int count=1;
for(int i=1;i<p.size();i++){
if(p[i]==p[i-1])count++;
else{
ans+=to_string(count);
ans+=p[i-1];
count=1;
}
}
ans+=to_string(count);
ans+=p[p.size()-1];
return ans;
}
string countAndSay(int n) {
return solve(n);
}

shobitsharma
Автор

any option to reduce the time complexity? This solution somehow looks naive.

dataman
Автор

Hi, I have one small doubt that why we add '&' or '$' at the end of the string, could you please explain this again?

kms
Автор

Nice Explanation! 1:04 pe hi samajh aa gya ki code kaise likhna hai :) .

Here is the code:

string GetNextNum(string num)
{
int ctr = 1;
int lastNum = num[0];
string nextNum;
int i;
for(i=1; i<num.size(); i++)
{
if(num[i] == lastNum)
{
ctr++;
}
else
{
nextNum += to_string(ctr) + num[i-1];
ctr=1;
lastNum = num[i];
}
}
nextNum += to_string(ctr) + num[i-1];
return nextNum;
}

string Solution::countAndSay(int A)
{
string nextNum = "1";
for(int i=0; i<A-1; i++)
{
string num = nextNum;
nextNum = GetNextNum(num);
}
return nextNum;
}

prathamgwari
Автор

keep doing it man, love your videos, will be great help for others also

SHAUMAYAOJHABCE
Автор

i think u should explain in brief why u are using delimiter.
that would be really helpful

lokeshnegi
Автор

//JAVA SOLUTION
class Solution {
public String countAndSay(int n) {
if(n<=0)
{
return null;
}

String result="1";
int i=1;

while(i<n)
{
StringBuilder sb=new StringBuilder();

int count=1;

for(int j=1;j<result.length();j++)
{

{
count++;
}
else
{
sb.append(count);



count=1;
}

}
sb.append(count);


result=sb.toString();
i++;
}
return result;
}
}

Rahul_Mongia
Автор

thanks a lot, Bhai !! The best explanation for this problem on youtube

satyapraneethkatta
Автор

Bro just a suggestion, plz explain the thorough intution, in the video there was just, about how to do the question, like this is the aldo and this is the code, there is no explanation to anything...

shubham
Автор

bro great explanation dry run bhi ho gya but yar ese think kese krna he matlab itna jyada sayad khudse kabhi soch hi nhi pata esa matlab esa sochna kese he bta skte he app ? plz

kirtanprajapati
Автор

for your code it's showing wrong answer, why??

kailashjangir
Автор

easiest solution..we don't need & or any $ symbol


..

class Solution {
public:
string countAndSay(int n)
{
if(n==1)
{
return "1";
}
if(n==2)
{
return "11";
}
string s="11";
int i;
for(int j=3;j<=n;j++)
{
string t="";
int count=1;
for( i=0;i<s.length()-1;i++)
{
if(s[i]!=s[i+1])
{
t=t+to_string(count);
t=t+s[i];
count=1;
}
else
{
count++;
}
}
t=t+to_string(count);
t=t+s[i];
s=t;
}
return s;
}

};

prashantsoni
Автор

i think its not the optimized approach as we are using nested loops

ajvlogs
Автор

bro why u have inserted delemter like $ in the string

gorakhkumargupta
Автор

In the 7th line, why does the loop start from 3 ?? What is the purpose of this loop?

karthikprabhu_career
Автор

Thank you so much love babbar cracker sheet shuru karne ke liye🙏🙏👍

pankajpatil