Day 11 LeetCode
👨‍💻

Day 11 LeetCode

Tags
Data Structures
Python
Java
Computer Science
Published
Jan 24, 2021

Day 11 Task:

  1. Two Sum: https://leetcode.com/problems/two-sum/
  1. Letter Combinations of a Phone Number: https://leetcode.com/problems/letter-combinations-of-a-phone-number/

Solutions:

Please review all the solutions and try implementing on your own if you haven’t gotten it.
 
#Two sum
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        num_map=dict()
        for i in range(len(nums)):
            complement=target-nums[i]
            if (complement in num_map):
                return [num_map[complement],i]
            else:
                num_map[nums[i]]=i
                print(num_map)
 
# Letter Combinations of a Phone Number
class Solution(object):
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        mappings = {"2":"abc","3":"def","4":"ghi","5":"jkl","6":"mno","7":"pqrs","8":"tuv","9":"wxyz"}
        result=[]
        
        def backtrack(i,curstr):
            if (len(curstr)==len(digits)):
                result.append(curstr)
                return
            
            for c in mappings[digits[i]]:
                backtrack(i+1,curstr + c)
                
        if digits:
                backtrack(0,"")
        return result