Reverse Count and Say


  • 0
    D

    Hi,

    I was recently asked the following question: Given an input string which is the output of a count and say method, return the original number.

    For example: If the number if "21", then the count and say method would return
    "1211" (one two, one one). In this problem, the input provided to us is "1211" and our goal is to return "21".

    I answered this question by approaching it as a variant of the WordBreak problem with a dynamic programming solution. Of course, the conditions to check the validity of the split string will differ. Thoughts?


  • 0
    C

    @deeptigp
    public static void main(String arg[]){
    String number ="1211";
    StringBuffer st = new StringBuffer();
    for (int i=1;i<number.length();){

            char co = (number.charAt(i-1));
            int count = Character.getNumericValue(co);
            char letter = number.charAt(i);
            for (int j=0;j<count;j++){
                st.append(letter);
            }
            i=i+2;
        }
        System.out.println(st);
    }

Log in to reply
 

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