Problem Statement

Pattern:


Solution

public String countAndSay(int n) {
	if (n <= 1) return "1";
	String num = countAndSay(n - 1);
	
	int count = 0;
	char curr = num.charAt(0);
	StringBuilder sb = new StringBuilder();
	
	for (char ch : num.toCharArray()) {
		if (ch == curr) count++;
		else {
			sb.append(count).append(curr) ;
			count = 1; curr = ch;
		}
	}
	sb.append(count).append(curr);
	return sb.toString();
}
 

TC : SC :

Notes