Posts

Showing posts from October, 2022

17. Letter Combinations of a Phone Number

Image
Difficulty: Medium  Given a string containing digits from   2-9   inclusive, return all possible letter combinations that the number could represent. Return the answer in   any order . A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.   Example 1: Input: digits = "23" Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] Example 2: Input: digits = "" Output: [] Example 3: Input: digits = "2" Output: ["a","b","c"] Python Solution: class Solution: def letterCombinations(self, digits: str) -> List[str]: d={2:'abc',3:'def',4:'ghi',5:'jkl',6:'mno',7:'pqrs',8:'tuv',9:'wxyz'} i=0 l=[] try: for i in d[int(digits[0])]: try:

7. Reverse Integer

Difficulty: Medium   Given a signed 32-bit integer   x , return   x  with its digits reversed . If reversing   x   causes the value to go outside the signed 32-bit integer range   [-2 31 , 2 31  - 1] , then return   0 . Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Example 1: Input: x = 123 Output: 321 Example 2: Input: x = -123 Output: -321 Example 3: Input: x = 120 Output: 21 Constraints: -2 31  <= x <= 2 31  - 1 Python Solution: class Solution: def reverse(self, x: int) -> int: x=str(x) x=x[::-1] if x[-1]=='-': if (-1)*int(x[:-1])<=2**31-1 and (-1)*int(x[:-1])>=-2**31: return (-1)*int(x[:-1]) else: return 0 else: if int(x)<=2**31-1 and int(x)>=-2**31: return int(x) else: return 0

118. Pascal's Triangle

Image
Difficulty: Easy   Given an integer   numRows , return the first numRows of   Pascal's triangle . In  Pascal's triangle , each number is the sum of the two numbers directly above it as shown:   Example 1: Input: numRows = 5 Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] Example 2: Input: numRows = 1 Output: [[1]] Python Solution : class Solution: def generate(self, numRows: int) -> List[List[int]]: l=[[1],[1,1]] if numRows==1: return [[1]] else: for _ in range(3,numRows+1): m=[1] for i in range(len(l[-1])-1): m.append(l[-1][i]+l[-1][i+1]) m.append(1) l.append(m) return l

12. Integer to Roman

Difficulty : Medium Roman numerals are represented by seven different symbols:  I ,  V ,  X ,  L ,  C ,  D  and  M . Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example,  2  is written as  II  in Roman numeral, just two one's added together.  12  is written as  XII , which is simply  X + II . The number  27  is written as  XXVII , which is  XX + V + II . Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not  IIII . Instead, the number four is written as  IV . Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as  IX . There are six instances where subtraction is used: I  can be placed before  V  (5) and  X  (10) to make 4 and 9.  X  can be placed before  L  (50) and  C  (100) to make 40 and 90.  C  can be placed before  D  (500) and  M  (1000) to mak

763. Partition Labels

Difficulty: Medium  You are given a string  s . We want to partition the string into as many parts as possible so that each letter appears in at most one part. Note that the partition is done so that after concatenating all the parts in order, the resultant string should be  s . Return  a list of integers representing the size of these parts . Example 1: Input: s = "ababcbacadefegdehijhklij" Output: [9,7,8] Explanation: The partition is "ababcbaca", "defegde", "hijhklij". This is a partition so that each letter appears in at most one part. A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts. Example 2: Input: s = "eccbbbbdec" Output: [10] Python SOlution: class Solution: def partitionLabels(self, s: str) -> List[int]: l=[0]*26 for i in range(len(s)): l[ord(s[i])-ord('a')]=i start,end=0,0 lst=[] for i