[java] LeetCode32
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- class Solution {
- public String countAndSay(int n) {
- String crtStr = "1"; // Base Case
- for (int i = 1; i < n; i++) {
- crtStr = converStr(crtStr);
- }
- return crtStr;
- }
- private String converStr(String str) {
- StringBuilder convertedStr = new StringBuilder("");
- char crtChar = str.charAt(0);
- int counter = 1;
- for (int i = 1; i < str.length(); i++) {
- if (str.charAt(i) == str.charAt(i - 1)) {
- counter++;
- } else {
- convertedStr.append(counter) ;
- convertedStr.append(crtChar);
- crtChar = str.charAt(i);
- counter = 0;
- }
- }
- convertedStr.append(counter);
- convertedStr.append(crtChar);
- return convertedStr.toString();
- }
- }
Editor
You can edit this paste and save as new: