├── Easy ├── 1. Two Sum.js ├── 1. Two Sum.py ├── 13. Roman to Integer.js ├── 13. Roman to Integer.py ├── 14. Longest Common Prefix.js ├── 14. Longest Common Prefix.py ├── 20. Valid Parentheses.js ├── 20. Valid Parentheses.py ├── 21. Merge Two Sorted Lists.js ├── 21. Merge Two Sorted Lists.py ├── 9. Palindrome Number.js └── 9. Palindrome Number.py └── README.md /Easy/1. Two Sum.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * 4 | Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. 5 | 6 | You may assume that each input would have exactly one solution, and you may not use the same element twice. 7 | 8 | You can return the answer in any order. 9 | 10 | 11 | 12 | Example 1: 13 | 14 | Input: nums = [2,7,11,15], target = 9 15 | Output: [0,1] 16 | Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. 17 | Example 2: 18 | 19 | Input: nums = [3,2,4], target = 6 20 | Output: [1,2] 21 | Example 3: 22 | 23 | Input: nums = [3,3], target = 6 24 | Output: [0,1] 25 | 26 | 27 | Constraints: 28 | 29 | 2 <= nums.length <= 104 30 | -109 <= nums[i] <= 109 31 | -109 <= target <= 109 32 | Only one valid answer exists. 33 | 34 | 35 | Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity? 36 | * 37 | */ 38 | 39 | // SOLUTION 1 ( O(N) ) 40 | const twoSum = function (nums, target) { 41 | const indexMap = {}; 42 | 43 | for (i in nums) { 44 | num = nums[i]; 45 | complement = target - num; 46 | 47 | if (indexMap[complement]) return [i, indexMap[complement]]; 48 | else indexMap[num] = i; 49 | } 50 | }; 51 | 52 | // SOLUTION 2 ( O(N) ) 53 | const twoSum = function (nums, target) { 54 | const complements = {}; 55 | 56 | for (i in nums) { 57 | num = nums[i]; 58 | complement = target - num; 59 | 60 | if (complements[num]) return [i, complements[num]]; 61 | else complements[complement] = i; 62 | } 63 | }; 64 | 65 | // SOLUTION 3 ( O(N^2) ) 66 | const twoSum = function (nums, target) { 67 | for (let i = 0; i < nums.length - 1; i++) { 68 | for (let j = i + 1; j < nums.length; j++) { 69 | if (nums[i] + nums[j] === target) return [i, j]; 70 | } 71 | } 72 | }; 73 | -------------------------------------------------------------------------------- /Easy/1. Two Sum.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. 3 | 4 | You may assume that each input would have exactly one solution, and you may not use the same element twice. 5 | 6 | You can return the answer in any order. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: nums = [2,7,11,15], target = 9 13 | Output: [0,1] 14 | Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. 15 | Example 2: 16 | 17 | Input: nums = [3,2,4], target = 6 18 | Output: [1,2] 19 | Example 3: 20 | 21 | Input: nums = [3,3], target = 6 22 | Output: [0,1] 23 | 24 | 25 | Constraints: 26 | 27 | 2 <= nums.length <= 104 28 | -109 <= nums[i] <= 109 29 | -109 <= target <= 109 30 | Only one valid answer exists. 31 | 32 | 33 | Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity? 34 | 35 | """ 36 | 37 | from typing import List 38 | 39 | class Solution: 40 | def twoSum(self, nums: List[int], target: int) -> List[int]: 41 | # Solution 1 ( O(N) ) 42 | indexMapper = {} 43 | for i, num in enumerate(nums): 44 | complement = target - num 45 | 46 | if indexMapper.get(complement) is not None: 47 | return [i, indexMapper[complement]] 48 | else: 49 | indexMapper[num] = i 50 | 51 | # Solution 2 ( O(N) ) 52 | complements = {} 53 | for i, num in enumerate(nums): 54 | complement = target - num 55 | 56 | if complements.get(num) is not None: 57 | return [i, complements[num]] 58 | else: 59 | complements[complement] = i 60 | 61 | # Solution 3 ( O(N ^ 2) ) 62 | for i in range(len(nums) - 1): 63 | for j in range(i + 1, len(nums)): 64 | if nums[i] + nums[j] == target: 65 | return [i, j] -------------------------------------------------------------------------------- /Easy/13. Roman to Integer.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * 4 | Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. 5 | Symbol Value 6 | I 1 7 | V 5 8 | X 10 9 | L 50 10 | C 100 11 | D 500 12 | M 1000 13 | 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. 14 | 15 | 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: 16 | 17 | I can be placed before V (5) and X (10) to make 4 and 9. 18 | X can be placed before L (50) and C (100) to make 40 and 90. 19 | C can be placed before D (500) and M (1000) to make 400 and 900. 20 | Given a roman numeral, convert it to an integer. 21 | 22 | 23 | 24 | Example 1: 25 | 26 | Input: s = "III" 27 | Output: 3 28 | Explanation: III = 3. 29 | Example 2: 30 | 31 | Input: s = "LVIII" 32 | Output: 58 33 | Explanation: L = 50, V= 5, III = 3. 34 | Example 3: 35 | 36 | Input: s = "MCMXCIV" 37 | Output: 1994 38 | Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. 39 | 40 | 41 | Constraints: 42 | 43 | 1 <= s.length <= 15 44 | s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M'). 45 | It is guaranteed that s is a valid roman numeral in the range [1, 3999]. 46 | * 47 | */ 48 | 49 | const romanToInt = function (s) { 50 | const symbols = { 51 | I: 1, 52 | V: 5, 53 | X: 10, 54 | L: 50, 55 | C: 100, 56 | D: 500, 57 | M: 1000, 58 | }; 59 | 60 | const specialSymbols = { 61 | I: { V: 4, X: 9 }, 62 | X: { L: 40, C: 90 }, 63 | C: { D: 400, M: 900 }, 64 | }; 65 | 66 | let result = 0; 67 | let pointer = 0; 68 | 69 | while (pointer < s.length) { 70 | let currentChar = s[pointer]; 71 | 72 | if (specialSymbols[currentChar]) { 73 | let nextChar = s[pointer + 1]; 74 | if (specialSymbols[currentChar][nextChar]) { 75 | result += specialSymbols[currentChar][nextChar]; 76 | pointer += 2; 77 | continue; 78 | } 79 | } 80 | 81 | // if it gets to this point, its a single character 82 | result += symbols[currentChar]; 83 | pointer++; 84 | } 85 | 86 | return result; 87 | }; 88 | -------------------------------------------------------------------------------- /Easy/13. Roman to Integer.py: -------------------------------------------------------------------------------- 1 | """ 2 | Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. 3 | Symbol Value 4 | I 1 5 | V 5 6 | X 10 7 | L 50 8 | C 100 9 | D 500 10 | M 1000 11 | 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. 12 | 13 | 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: 14 | 15 | I can be placed before V (5) and X (10) to make 4 and 9. 16 | X can be placed before L (50) and C (100) to make 40 and 90. 17 | C can be placed before D (500) and M (1000) to make 400 and 900. 18 | Given a roman numeral, convert it to an integer. 19 | 20 | 21 | 22 | Example 1: 23 | 24 | Input: s = "III" 25 | Output: 3 26 | Explanation: III = 3. 27 | Example 2: 28 | 29 | Input: s = "LVIII" 30 | Output: 58 31 | Explanation: L = 50, V= 5, III = 3. 32 | Example 3: 33 | 34 | Input: s = "MCMXCIV" 35 | Output: 1994 36 | Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. 37 | 38 | 39 | Constraints: 40 | 41 | 1 <= s.length <= 15 42 | s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M'). 43 | It is guaranteed that s is a valid roman numeral in the range [1, 3999]. 44 | 45 | """ 46 | class Solution: 47 | def romanToInt(self, s: str) -> int: 48 | symbols = { 49 | "I": 1, 50 | "V": 5, 51 | "X": 10, 52 | "L": 50, 53 | "C": 100, 54 | "D": 500, 55 | "M": 1000, 56 | } 57 | 58 | special_symbols = { 59 | "I": {"V": 4, "X": 9}, 60 | "X": {"L": 40, "C": 90}, 61 | "C": {"D": 400, "M": 900} 62 | } 63 | 64 | pointer = 0 65 | result = 0 66 | 67 | while pointer < len(s): 68 | current_char = s[pointer] 69 | 70 | if special_symbols.get(current_char): 71 | if pointer + 1 < len(s): 72 | next_char = s[pointer + 1] 73 | else: 74 | next_char = None 75 | 76 | if special_symbols[current_char].get(next_char): 77 | result = result + special_symbols[current_char][next_char] 78 | pointer = pointer + 2 79 | continue 80 | 81 | # if it gets to this point, handle as a single character 82 | result = result + symbols[current_char] 83 | pointer = pointer + 1 84 | 85 | return result 86 | -------------------------------------------------------------------------------- /Easy/14. Longest Common Prefix.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | Write a function to find the longest common prefix string amongst an array of strings. 4 | 5 | If there is no common prefix, return an empty string "". 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: strs = ["flower","flow","flight"] 12 | Output: "fl" 13 | Example 2: 14 | 15 | Input: strs = ["dog","racecar","car"] 16 | Output: "" 17 | Explanation: There is no common prefix among the input strings. 18 | 19 | 20 | Constraints: 21 | 22 | 1 <= strs.length <= 200 23 | 0 <= strs[i].length <= 200 24 | strs[i] consists of only lower-case English letters. 25 | * 26 | */ 27 | 28 | // Solution ( O(nlogn) ) 29 | const longestCommonPrefix = function (strs) { 30 | if (strs.length === 1) return strs[0]; 31 | 32 | strs.sort((str1, str2) => (str1 > str2 ? 1 : -1)); 33 | 34 | const first = strs[0]; 35 | const last = strs[strs.length - 1]; 36 | 37 | let result = ""; 38 | 39 | for (let i = 0; i < first.length; i++) { 40 | if (first[i] === last[i]) result += first[i]; 41 | else break; 42 | } 43 | 44 | return result; 45 | }; 46 | -------------------------------------------------------------------------------- /Easy/14. Longest Common Prefix.py: -------------------------------------------------------------------------------- 1 | """ 2 | Write a function to find the longest common prefix string amongst an array of strings. 3 | 4 | If there is no common prefix, return an empty string "". 5 | 6 | 7 | 8 | Example 1: 9 | 10 | Input: strs = ["flower","flow","flight"] 11 | Output: "fl" 12 | Example 2: 13 | 14 | Input: strs = ["dog","racecar","car"] 15 | Output: "" 16 | Explanation: There is no common prefix among the input strings. 17 | 18 | 19 | Constraints: 20 | 21 | 1 <= strs.length <= 200 22 | 0 <= strs[i].length <= 200 23 | strs[i] consists of only lower-case English letters. 24 | """ 25 | 26 | from typing import List 27 | 28 | class Solution: 29 | def longestCommonPrefix(self, strs: List[str]) -> str: 30 | # Solution ( O(nlogn) ) 31 | if len(strs) == 1: 32 | return strs[0] 33 | 34 | strs.sort() 35 | 36 | result = "" 37 | first = strs[0] 38 | last = strs[len(strs) - 1] 39 | 40 | for i in range(len(first)): 41 | if first[i] == last[i]: 42 | result = result + first[i] 43 | else: 44 | break 45 | 46 | return result 47 | -------------------------------------------------------------------------------- /Easy/20. Valid Parentheses.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 4 | 5 | An input string is valid if: 6 | 7 | Open brackets must be closed by the same type of brackets. 8 | Open brackets must be closed in the correct order. 9 | 10 | 11 | Example 1: 12 | 13 | Input: s = "()" 14 | Output: true 15 | Example 2: 16 | 17 | Input: s = "()[]{}" 18 | Output: true 19 | Example 3: 20 | 21 | Input: s = "(]" 22 | Output: false 23 | 24 | 25 | Constraints: 26 | 27 | 1 <= s.length <= 104 28 | s consists of parentheses only '()[]{}'. 29 | * 30 | */ 31 | 32 | const isValid = function (s) { 33 | const stack = []; 34 | 35 | const bracketMapping = { 36 | ")": "(", 37 | "}": "{", 38 | "]": "[", 39 | }; 40 | 41 | for (let char of s) { 42 | if (bracketMapping[char]) { 43 | const popped = stack.pop(); 44 | if (popped !== bracketMapping[char]) return false; 45 | } else { 46 | stack.push(char); 47 | } 48 | } 49 | 50 | return !stack.length; 51 | }; 52 | -------------------------------------------------------------------------------- /Easy/20. Valid Parentheses.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 3 | 4 | An input string is valid if: 5 | 6 | Open brackets must be closed by the same type of brackets. 7 | Open brackets must be closed in the correct order. 8 | 9 | 10 | Example 1: 11 | 12 | Input: s = "()" 13 | Output: true 14 | Example 2: 15 | 16 | Input: s = "()[]{}" 17 | Output: true 18 | Example 3: 19 | 20 | Input: s = "(]" 21 | Output: false 22 | 23 | 24 | Constraints: 25 | 26 | 1 <= s.length <= 104 27 | s consists of parentheses only '()[]{}'. 28 | """ 29 | 30 | class Solution: 31 | def isValid(self, s: str) -> bool: 32 | 33 | stack = [] 34 | bracket_mapping = { 35 | ")": "(", 36 | "}": "{", 37 | "]": "[" 38 | } 39 | 40 | for char in s: 41 | if bracket_mapping.get(char): 42 | try: 43 | popped = stack.pop() 44 | if popped != bracket_mapping[char]: 45 | return False 46 | except IndexError: 47 | return False 48 | else: 49 | stack.append(char) 50 | 51 | return not len(stack) 52 | -------------------------------------------------------------------------------- /Easy/21. Merge Two Sorted Lists.js: -------------------------------------------------------------------------------- 1 | /* 2 | You are given the heads of two sorted linked lists list1 and list2. 3 | 4 | Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. 5 | 6 | Return the head of the merged linked list. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | 13 | Input: list1 = [1,2,4], list2 = [1,3,4] 14 | Output: [1,1,2,3,4,4] 15 | Example 2: 16 | 17 | Input: list1 = [], list2 = [] 18 | Output: [] 19 | Example 3: 20 | 21 | Input: list1 = [], list2 = [0] 22 | Output: [0] 23 | 24 | 25 | Constraints: 26 | 27 | The number of nodes in both lists is in the range [0, 50]. 28 | -100 <= Node.val <= 100 29 | Both list1 and list2 are sorted in non-decreasing order. 30 | 31 | */ 32 | 33 | function ListNode(val, next) { 34 | this.val = val === undefined ? 0 : val; 35 | this.next = next === undefined ? null : next; 36 | } 37 | 38 | var mergeTwoLists = function (list1, list2) { 39 | if (!list1) return list2; 40 | if (!list2) return list1; 41 | 42 | const result = new ListNode(); 43 | let resultTail = result; 44 | p1 = list1; 45 | p2 = list2; 46 | 47 | while (p1 && p2) { 48 | if (p1.val > p2.val) { 49 | resultTail.next = new ListNode(p2.val); 50 | resultTail = resultTail.next; 51 | p2 = p2.next; 52 | } else { 53 | resultTail.next = new ListNode(p1.val); 54 | resultTail = resultTail.next; 55 | p1 = p1.next; 56 | } 57 | } 58 | 59 | if (p1) { 60 | resultTail.next = p1; 61 | } 62 | 63 | if (p2) { 64 | resultTail.next = p2; 65 | } 66 | 67 | return result.next; 68 | }; 69 | -------------------------------------------------------------------------------- /Easy/21. Merge Two Sorted Lists.py: -------------------------------------------------------------------------------- 1 | """ 2 | You are given the heads of two sorted linked lists list1 and list2. 3 | 4 | Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. 5 | 6 | Return the head of the merged linked list. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | 13 | Input: list1 = [1,2,4], list2 = [1,3,4] 14 | Output: [1,1,2,3,4,4] 15 | Example 2: 16 | 17 | Input: list1 = [], list2 = [] 18 | Output: [] 19 | Example 3: 20 | 21 | Input: list1 = [], list2 = [0] 22 | Output: [0] 23 | 24 | 25 | Constraints: 26 | 27 | The number of nodes in both lists is in the range [0, 50]. 28 | -100 <= Node.val <= 100 29 | Both list1 and list2 are sorted in non-decreasing order. 30 | """ 31 | 32 | from typing import List, Optional 33 | 34 | # Definition for singly-linked list. 35 | class ListNode: 36 | def __init__(self, val=0, next=None): 37 | self.val = val 38 | self.next = next 39 | 40 | class Solution: 41 | def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: 42 | if not list1: 43 | return list2 44 | 45 | if not list2: 46 | return list1 47 | 48 | result = ListNode() 49 | result_tail = result 50 | p1 = list1 51 | p2 = list2 52 | 53 | while p1 and p2: 54 | if p1.val > p2.val: 55 | result_tail.next = ListNode(p2.val) 56 | result_tail = result_tail.next 57 | p2 = p2.next 58 | else: 59 | result_tail.next = ListNode(p1.val) 60 | result_tail = result_tail.next 61 | p1 = p1.next 62 | 63 | if p1: 64 | result_tail.next = p1 65 | 66 | if p2: 67 | result_tail.next = p2 68 | 69 | return result.next 70 | 71 | -------------------------------------------------------------------------------- /Easy/9. Palindrome Number.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * 4 | Given an integer x, return true if x is palindrome integer. 5 | 6 | An integer is a palindrome when it reads the same backward as forward. 7 | 8 | For example, 121 is a palindrome while 123 is not. 9 | 10 | 11 | Example 1: 12 | 13 | Input: x = 121 14 | Output: true 15 | Explanation: 121 reads as 121 from left to right and from right to left. 16 | Example 2: 17 | 18 | Input: x = -121 19 | Output: false 20 | Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. 21 | * 22 | * 23 | */ 24 | 25 | // Solution 2( O(n) ) 26 | var isPalindrome = function (x) { 27 | x = String(x); 28 | 29 | let p1 = 0; 30 | let p2 = x.length - 1; 31 | 32 | while (p1 <= p2) { 33 | if (x[p1] !== x[p2]) return false; 34 | else { 35 | p1++; 36 | p2--; 37 | } 38 | } 39 | 40 | return true; 41 | }; 42 | 43 | // Solution (Recursive) 44 | var isPalindrome = function (x) { 45 | if (typeof x !== "string") x = String(x); 46 | 47 | if (!x) return true; // base case 48 | 49 | if (x[0] === x[x.length - 1]) return isPalindrome(x.slice(1, x.length - 1)); 50 | else return false; 51 | }; 52 | -------------------------------------------------------------------------------- /Easy/9. Palindrome Number.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an integer x, return true if x is palindrome integer. 3 | 4 | An integer is a palindrome when it reads the same backward as forward. 5 | 6 | For example, 121 is a palindrome while 123 is not. 7 | 8 | 9 | Example 1: 10 | 11 | Input: x = 121 12 | Output: true 13 | Explanation: 121 reads as 121 from left to right and from right to left. 14 | Example 2: 15 | 16 | Input: x = -121 17 | Output: false 18 | Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. 19 | """ 20 | 21 | class Solution: 22 | def isPalindrome(self, x: int) -> bool: 23 | # Solution ( O(N) ) 24 | x = str(x) 25 | 26 | p1 = 0 27 | p2 = len(x) - 1 28 | 29 | while p1 <= p2: 30 | if x[p1] != x[p2]: 31 | return False 32 | else: 33 | p1 = p1 + 1 34 | p2 = p2 - 1 35 | 36 | return True 37 | 38 | # Solution (Recursive) 39 | if type(x) != str: 40 | x = str(x) 41 | 42 | if not x: 43 | return True 44 | 45 | if x[0] == x[len(x) - 1]: 46 | return self.isPalindrome(x[1:len(x) - 1]) 47 | else: 48 | return False 49 | 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Leetcode Premium Solutions 2 | 3 | Personal Javascript/Python solutions to Leetcode problems. 4 | --------------------------------------------------------------------------------