Simple Java Code


  • 0
    V

    public class Solution {
    public String countAndSay(int n) {
    if (n <= 0)
    return null;

    String initial = "1";
    int i = 1;
       while (i < n)
        {
    	StringBuilder word = new StringBuilder();
    	int count = 1;
    	for (int j = 1; j < initial.length(); j++) {
    		if (initial.charAt(j) == initial.charAt(j - 1)) {
    			count++;
    		} else {
    			word.append(count);
    			word.append(initial.charAt(j - 1));
    			count = 1;
    		}
    	}
    

    word.append(count);
    word.append(initial.charAt(initial.length() - 1));
    initial = word.toString();
    i++;
    }

    return initial;
    

    }

    }


Log in to reply
 

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