package com.freetymekiyan.algorithms.level.easy;

/**
 * The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
 * 1 is read off as "one 1" or 11.
 * 11 is read off as "two 1s" or 21.
 * 21 is read off as "one 2, then one 1" or 1211.
 * Given an integer n, generate the nth sequence.
 * Note: The sequence of integers will be represented as a string.
 * Note: brute way: loop string
 *
 * @author chenshuna
 */

class CountAndSay {

    public static String countAndSay(int n) {
        String restult = "1";
        for (int i = 1; i < n; i++) {
            restult = countAndSayArray(restult);
        }
        return restult;
    }

    public static String countAndSayArray(String cas) {
        int n = cas.length();
        int count = 1;
        int point = 0;
        StringBuilder res = new StringBuilder();
        while (point < n) {
            while ((point + count) < n && cas.charAt(point) == cas.charAt(point + count)) {
                count++;
            }
            res.append(count + "");
            res.append(cas.charAt(point) + "");
            point = point + count;
            count = 1;
        }

        return res.toString();

    }

    public static void main(String[] args) {
        System.out.print(countAndSay(4));
    }

}
@@ -126,16 +126,20 @@
 follows:
+%0A * 
 1, 11, 2
@@ -159,16 +159,23 @@
 21, ...%0A
+ * %3Cp%3E%0A
  * 1 is 
@@ -338,16 +338,23 @@
 quence.%0A
+ * %3Cp%3E%0A
  * Note:
@@ -419,102 +419,252 @@
 %0A * 
-Note: brute way: loop string%0A *%0A * @author chenshuna%0A */%0A%0Aclass CountAndSay %7B%0A%0A    public stat
+%3Cp%3E%0A * Company Tags: Facebook%0A * Tags: String%0A * Similar Problems: (M) Encode and Decode Strings%0A */%0A%0Aclass CountAndSay %7B%0A%0A    /**%0A     * String. Simulate the process.%0A     * Generate the next sequence.%0A     * Repeat n-1 times.%0A     */%0A    publ
 ic S
@@ -708,20 +708,16 @@
 ring res
-tult
  = %221%22;%0A
@@ -728,84 +728,76 @@
     
-for (int i = 1; i %3C n; i++) %7B%0A            restult = countAndSayArray(restult
+while (--n %3E 0) %7B%0A            StringBuilder seq = new StringBuilder(
 );%0A 
@@ -807,150 +807,105 @@
     
-%7D%0A
     
-    return restult
+char%5B%5D prev = res.toCharArray()
 ;%0A    
-%7D%0A%0A
+ 
     
-public static String countAndSayArray(String cas) %7B%0A        int n = cas.length();%0A        int count = 1;%0A
+   for (int i = 0; i %3C prev.length; i++) %7B%0A        
     
@@ -916,188 +916,128 @@
 int 
-poi
+cou
 nt = 
-0;%0A        StringBuilder res = new StringBuilder();%0A        while (point %3C n) %7B%0A            while ((point + count) %3C n && cas.charAt(point) == cas.charAt(point + count)) %7B%0A
+1; // Get count of same characters.%0A                while (i + 1 %3C prev.length && prev%5Bi%5D == prev%5Bi + 1%5D) %7B%0A    
     
@@ -1073,18 +1073,16 @@
     
-%7D%0A
         
     
@@ -1081,171 +1081,117 @@
     
-    res.append(count + %22%22);%0A            res.append(cas.charAt(point) + %22%22);%0A            point = point + count;%0A            count = 1;%0A        %7D%0A%0A
+i++;%0A                %7D%0A                seq.append(count).append(prev%5Bi%5D);%0A            %7D%0A     
        
- 
 re
-turn res
+s = seq
 .toS
@@ -1203,107 +1203,42 @@
 ();%0A
-%0A
     
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        System.out.print(countAndSay(4))
+%7D%0A        return res
 ;%0A    %7D%0A
 %0A%7D
@@ -1235,10 +1235,9 @@
 ;%0A    %7D%0A
-%0A
 %7D

update count and say