Leetcode: Count and Say

The following code performs the following question:


The count-and-say sequence is a sequence of digit strings defined by the recursive formula:

  • countAndSay(1) = "1"
  • countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is then converted into a different digit string.

To determine how you "say" a digit string, split it into the minimal number of groups so that each group is a contiguous section all of the same character. Then for each group, say the number of characters, then say the character. To convert the saying into a digit string, replace the counts with a number and concatenate every saying.

For example, the saying and conversion for digit string "3322251":

Given a positive integer n, return the nth term of the count-and-say sequence.


class Solution {

    public String countAndSay(int n) {


        //base cases is when n = 0 and n = 1. This indicates that 

        if (n <= 0) return null;

        if (n == 1) return "1";


        //initial return value

        String result = "1";


        //initialize the string pointer to 1. 

        int i = 1;


        //go accross all String Pointers, and then make the Result String different

        while (i < n) {


            //increment the number, which is given in the input. 

            i = i + 1;


            //something easy to use to concatenate to the string

            StringBuilder sb = new StringBuilder();

            

            //initialize the count variable, that counts the number of occurences of a specific number. 

            int count = 1;

            

            

            for (int j = 1; j < result.length(); j++) {

                

                //if there is a repeat character, then increment the number of occurrences. 

                

                if (result.charAt(j) == result.charAt(j - 1)) {

                    count = count + 1;

                }

                

                //else, append the count, and go to the next character. 

                

                else {

                    sb.append(count);

                    sb.append(result.charAt(j - 1));

                    count = 1;

                }

            }


            //append the last string in the result to the StringBuilder

            sb.append(count);

            sb.append(result.charAt(result.length() - 1));

            //convert the stringBuilder to the String. 

            result = sb.toString();

        }

        

        //return the following result string. 

        return result;

    }

}

Comments

Popular Posts