14-line solution using recursion in java


  • 0
       public static String countAndSay(int n) {
                    String s = "";
    		if (n==1) return "1";
    		String prev = countAndSay(n-1);
    		int ct;
    		char [] c = prev.toCharArray();
    		for (int i = 0; i<c.length; ++i ){
    			ct = 1;
    			for (;i<c.length-1 && c[i]==c[i+1];) {
    				ct++;
    				i++;  
    			}
    			s+=ct+""+c[i];
    		}
    		return s;
        }

Log in to reply
 

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