MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / countAndSay

Method countAndSay

LeetCode_problems/Count and Say/solution.cpp:1–34  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1string Solution::countAndSay(int A) {
2 string s[A+2];
3 // initialising strings
4 s[1] = "1";
5 s[2] = "11";
6 for(int i = 3; i <= A; i++) {
7 s[i] = "";
8 // To find identical consecutive characters
9 for(int j = 0; j < s[i-1].length(); j++) {
10 char x = s[i-1][j];
11 int count = 1;
12 while((j+1) < s[i-1].length() && s[i-1][j+1] == x) {
13 count++;
14 j++;
15 }
16 // Counting letter occurences
17 string sCount = "";
18 while(count > 0) {
19 int y = count % 10;
20 count = count / 10;
21 char c = y + '0';
22 sCount.push_back(c);
23 }
24 // Adding the letters
25 string iCount = "";
26 for(int j = sCount.length() - 1; j >= 0; j--) {
27 iCount += sCount[j];
28 }
29 s[i] += iCount;
30 s[i] += x;
31 }
32 }
33 return s[A];
34}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected