Count and Say - Solution


  • 0
    A

    var countAndSay = function(iteration) {

      if(iteration == 1) return '1';
      if(iteration == 2) return '11';
    
      let start = '1'
      for (let i = 0; i < iteration-1; i++) {
        // fill-in the counter
        let counter = 0;
        let char = undefined;
        let result = '';
    
        for (let j = 0; j < start.length; j++) {
          if (start[j] !== char) {
            if (counter > 0) {
              result += counter + char;
            }
            counter = 0;
            char = start[j];
          }
          counter++;
        }
        if (counter > 0) {
          result += counter + char;
        }
        start = result;
      }
      return start;
    

    };


Log in to reply
 

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