public String countAndSay(int n) {

    StringBuffer sb = new StringBuffer("1");

    for (int i = 1 ; i < n ; i++) {
      String str = sb.toString();
      sb.setLength(0);
      int count = 1;
      for (int j = 1 ; j <= str.length() ; j++) {

        if (j == str.length()) {
          sb.append(count).append(str.charAt(j - 1));
        } else if (str.charAt(j) == str.charAt(j - 1)) {
          count++;
        } else {
          sb.append(count).append(str.charAt(j - 1));
          count = 1;
        }
      }
    }

    return sb.toString();
  }