Google Interview Question

Write a function that given a sequence and a number b between [-10,10] return a new sequence. Sequences are generated by this: http://en.wikipedia.org/wiki/Look-and-say_sequence a number b if equal to 0 the function will return the input sequence Valid sequences: 1 11 21 1211 111221 ... Example: input: 1211, +1 output: 111221 Example: input: 111221, -1 output: 1211

Interview Answers

Anonymous

Nov 17, 2014

Used recursion to generate sequences. Should be an iterative way to do the generation, but I was able to figure it out during the interview.

Anonymous

Mar 9, 2015

static String look_and_Say_add_1(String num){ if(num==null) return null; StringBuilder sb = new StringBuilder(); char repeat = num.charAt(0); num = num.substring(1) + " "; int times = 1; for(char current: num.toCharArray()){ if(current==repeat){ times++; } else{ sb.append(times+""+repeat); times = 1; repeat = current; } } return sb.toString(); }