MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / Solution

Class Solution

CountandSay.java:2–30  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1// Approach 1: Recursion!
2class Solution {
3 public String countAndSay(int n) {
4 if(n==1) return "1";
5 String s=countAndSay(n-1);
6 String res="";
7 int count=1,flag=0;
8 for(int i=0;i<s.length()-1;i++)
9 {
10 if(s.charAt(i)==s.charAt(i+1))
11 {
12 count++;
13 flag=i+1;
14 }
15 else{
16 res=res+count+""+s.charAt(i);
17 count=1;
18 }
19 }
20 if(flag+1==s.length()){
21 res=res+count+""+s.charAt(flag);
22 }
23 else
24 {
25 res=res+"1"+s.charAt(s.length()-1);
26 }
27 return res;
28 }
29
30}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected