Ruby solution - BFS


  • 0
    N
    # @param {String} digits
    # @return {String[]}
    def letter_combinations(digits)
        
        return [] if digits == ''
        
      mapping = ['0', '1', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
      
      solution = [""]
      
      for i in 0...digits.length
        digit = digits[i].to_i
        
        while solution[0].length == i
            combination = solution.shift
            mapping[digit].chars.each do |char|
                solution.push(combination + char)
            end
        end
      end
      
      solution
      
    end
    

Log in to reply
 

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