Simple Brute Force Approach


  • 0
    S
        public String countAndSay(int n) {
            String num="1",nextnum="";
            int count=0;
            
            if(n==1)
                return num;
            
            for(int i=0;i<n-1;++i){
                char ch = num.charAt(0);
                count=1;
                for(int j=1;j<num.length();++j){
                    if(num.charAt(j)==ch)
                        ++count;
                    else{
                        nextnum+=count+""+ch+"";
                        count=1;
                        ch = num.charAt(j);
                    }
                }
                nextnum+=count+""+ch+"";
                num = nextnum;
                nextnum="";
            }
            
            return num;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.