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

Class Solution

CountAndSay.java:1–26  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2 public String countAndSay(int n) {
3
4 if(n==1) return "1";
5// Recursion
6 String s=countAndSay(n-1);
7 StringBuilder res = new StringBuilder();
8 // String res="";
9 int counter=0;
10
11 for(int i=0;i<s.length();i++)
12 {
13 counter++;
14// Segregating into groups
15 if(i==s.length()-1 || s.charAt(i)!=s.charAt(i+1))
16 {
17 res.append(counter).append(s.charAt(i));
18 // res=res+counter+s.charAt(i);
19 counter=0;
20 }
21 }
22 return res.toString();
23
24
25 }
26}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected