🤖

LeetCode

Motivation

I need a job, please.

Solutions

Arrays & Hashing

3110. Score of a String | Easy
Linear time and constant space
class Solution: def scoreOfString(self, s: str) -> int: result = 0 for i in range(len(s) - 1): result += abs(ord(s[i]) - ord(s[i + 1])) return result
1929. Concatenation of Array | Easy
Two pass, linear time and linear space
class Solution: def getConcatenation(self, nums: List[int]) -> List[int]: result = [] for n in nums: result.append(n) for n in nums: result.append(n) return result
Single pass, linear time and linear space
class Solution: def getConcatenation(self, nums: List[int]) -> List[int]: length = len(nums) result = [0] * (2 * length) for i, n in enumerate(nums): result[i] = result[i + length] = n return result
217. Contains Duplicate | Easy
Brute force, quadratic time and constant space
class Solution: def containsDuplicate(self, nums: List[int]) -> bool: length = len(nums) for i in range(length): for j in range(i + 1, length): if nums[i] == nums[j]: return True return False
Sorting, linearithmic time and linear space
class Solution: def containsDuplicate(self, nums: List[int]) -> bool: nums.sort() length = len(nums) for i in range(length - 1): if nums[i] == nums[i + 1]: return True return False
Hash set, linear time and linear space
class Solution: def containsDuplicate(self, nums: List[int]) -> bool: seen = set() for n in nums: if n in seen: return True else: seen.add(n) return False
Hash set length, linear time and linear space
class Solution: def containsDuplicate(self, nums: List[int]) -> bool: return len(set(nums)) < len(nums)
242. Valid Anagram | Easy
Sorting, linearithmic time and linear space
class Solution: def isAnagram(self, s: str, t: str) -> bool: return sorted(s) == sorted(t)
Hash map, linear time and constant space
class Solution: def isAnagram(self, s: str, t: str) -> bool: if len(s) != len(t): return False countS = {} countT = {} length = len(s) for i in range(length): countS[s[i]] = 1 + countS.get(s[i], 0) countT[t[i]] = 1 + countT.get(t[i], 0) return countS == countT
Hash map with array, linear time and constant space
class Solution: def isAnagram(self, s: str, t: str) -> bool: if len(s) != len(t): return False count = [0] * 26 length = len(s) for i in range(length): count[ord(s[i]) - ord("a")] += 1 count[ord(t[i]) - ord("a")] -= 1 for i in range(26): if count[i] != 0: return False return True
1299. Replace Elements with Greatest Element on Right Side | Easy
Brute force, quadratic time and constant space
class Solution: def replaceElements(self, arr: List[int]) -> List[int]: length = len(arr) answer = [0] * length for i in range(length): rightMax = -1 for j in range(i + 1, length): rightMax = max(rightMax, arr[j]) answer[i] = rightMax return answer
Suffix max, linear time and constant space
class Solution: def replaceElements(self, arr: List[int]) -> List[int]: length = len(arr) answer = [0] * length rightMax = -1 for i in range(length - 1, -1, -1): answer[i] = rightMax rightMax = max(rightMax, arr[i]) return answer
392. Is Subsequence | Easy
Two pointers, linear time and constant space
class Solution: def isSubsequence(self, s: str, t: str) -> bool: i, j = 0, 0 while i < len(s) and j < len(t): if s[i] == t[j]: i += 1 j += 1 return i == len(s)
2486. Append Characters to String to Make Subsequence | Medium
Two pointers, linear time and constant space
class Solution: def appendCharacters(self, s: str, t: str) -> int: i, j = 0, 0 while i < len(s) and j < len(t): if s[i] == t[j]: j += 1 i += 1 return len(t) - j
58. Length of Last Word | Easy
Split function, linear time and linear space
class Solution: def lengthOfLastWord(self, s: str) -> int: return len(s.split()[-1])
Front to end scan, linear time and constant space
class Solution: def lengthOfLastWord(self, s: str) -> int: i, ans = 0, 0 while i < len(s): if s[i] == " ": while i < len(s) and s[i] == " ": i += 1 if i == len(s): break ans = 0 else: i += 1 ans += 1 return ans
Back to front scan, linear time and constant space
class Solution: def lengthOfLastWord(self, s: str) -> int: i, ans = len(s) - 1, 0 while s[i] == " ": i -= 1 while i >= 0: if s[i] == " ": break i -= 1 ans += 1 return ans
2678. Number of Senior Citizens | Easy
String parsing, linear time and constant space
class Solution: def countSeniors(self, details: List[str]) -> int: ans = 0 for passenger in details: age = int(passenger[11:13]) if age > 60: ans += 1 return ans
1. Two Sum | Easy
Brute force, quadratic time and constant space
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)): for j in range(i, len(nums)): if nums[i] + nums[j] == target: return [i, j]
Hash map, linear time and linear space
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: map = {} for i in range(len(nums)): diff = target - nums[i] if diff in map: return [map[diff], i] else: map[nums[i]] = i
14. Longest Common Prefix | Easy
Horizontal scanning, quadratic time and constant space
class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: result = "" for i in range(len(strs[0])): for str in strs: if i == len(str) or strs[0][i] != str[i]: return result result += strs[0][i] return result
1408. String Matching in an Array | Easy | Medium
Brute force, quadratic time and linear space
class Solution: def stringMatching(self, words: List[str]) -> List[str]: answer = [] for i in range(len(words)): for j in range(len(words)): if i == j: continue if words[i] in words[j]: answer.append(words[i]) break return answer
49. Group Anagrams | Medium
Hash map, quadratic time and linear space
class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: result = defaultdict(list) for str in strs: charMap = [0] * 26 for letter in str: charMap[ord(letter) - ord("a")] += 1 result[tuple(charMap)].append(str) return list(result.values())
118. Pascal's Triangle | Easy
Array scan, quadratic time and constant space
class Solution: def generate(self, numRows: int) -> List[List[int]]: result = [[1]] for i in range(numRows - 1): newRow = [] tempRow = [0] + result[-1] + [0] for j in range(len(result[-1]) + 1): newRow.append(tempRow[j] + tempRow[j + 1]) result.append(newRow) return result
27. Remove Element | Easy
Array scan, linear time and constant space
class Solution: def removeElement(self, nums: List[int], val: int) -> int: k = 0 for num in nums: if num != val: nums[k] = num k += 1 return k

References