Two Pointers
Valid Palindrome (Easy)
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
def isPalindrome(s: str) -> bool: s = s.lower() left, right = 0, len(s) - 1 while left < right: if not s[left].isalnum(): left += 1 elif not s[right].isalnum(): right -= 1 elif s[left] != s[right]: return False else: left += 1 right -= 1 return True
Time: O(n)
Space: O(1)
Is Subsequence (Easy)
Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
def isSubsequence(s: str, t: str) -> bool: if not s: return True i = 0 for char in t: if char == s[i]: i += 1 if i == len(s): return True return False
Time: O(n)
Space: O(1)
Two Sum II - Input Array Is Sorted (Medium)
Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.
Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
Assume there is exactly one solution. Do not use the same element twice. The solution must use only constant extra space.
def twoSum(numbers: List[int], target: int) -> List[int]: left, right = 0, len(numbers) - 1 while left < right: currSum = numbers[left] + numbers[right] if currSum > target: right -= 1 elif currSum < target: left += 1 else: return [left + 1, right + 1]
Time: O(n)
Space: O(1)
Container With Most Water (Medium)
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
def maxArea(height: List[int]) -> int: left, right = 0, len(height) - 1 answer = 0 # Track max area while shrinking two pointers while left < right: answer = max(answer, min(height[left], height[right]) * (right - left)) if height[left] < height[right]: left += 1 else: right -= 1 return answer
Time: O(n)
Space: O(1)
3Sum (Medium)
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
The solution set must not contain duplicate triplets.
def threeSum(nums: List[int]) -> List[List[int]]: nums.sort() answer = [] # Similar to 2Sum. Use sorted list and skip duplicates for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i - 1]: continue left, right = i + 1, len(nums) - 1 while left < right: tripleSum = nums[i] + nums[left] + nums[right] if tripleSum > 0: right -= 1 elif tripleSum < 0: left += 1 else: answer.append([nums[i], nums[left], nums[right]]) while left < right and nums[left] == nums[left + 1]: left += 1 while left < right and nums[right] == nums[right - 1]: right -= 1 right -= 1 left += 1 return answer
Time: O(n2)
Space: O(1)