Neetcode 150 is a collection of 150 coding problems for software engineers and developers to practice their coding skills. The problems are carefully selected to cover a wide range of topics and difficulty levels, making it an ideal resource for interview preparation and skill development.
nums, return true if any value appears more than once in the array, otherwise return false.Example 1:
Example 2:
true.set data structure to store the elements and then compare the lengths of the original array and the set.# Pythonic Solution {#pythonic-solution}
def containsDuplicate(nums):
return len(nums) != len(set(nums))
# Sorting {#sorting}
def containsDuplicate(nums):
nums.sort()
for i in range(1, len(nums)):
if nums[i] == nums[i - 1]:
return True
return False
# Hash Set {#hash-set}
def containsDuplicate(nums):
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False
# Brute Force {#brute-force}
def containsDuplicate(nums):
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] == nums[j]:
return True
return False
Given two strings s and t, return true if the two strings are anagrams of each other, otherwise return false.
An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.
Example 1:
Example 2:
# Pythonic Solution {#pythonic-solution}
from collections import Counter
def isAnagram(s, t):
return Counter(s) == Counter(t)
# Hash Map {#hash-map}
def isAnagram(s, t):
if len(s) != len(t):
return False
freq_s = {}
freq_t = {}
for char in s:
freq_s[char] = freq_s.get(char, 0) + 1
for char in t:
freq_t[char] = freq_t.get(char, 0) + 1
return freq_s == freq_t
# Sorting {#sorting}
def isAnagram(s, t):
return sorted(s) == sorted(t)
Given an array of integers nums and an integer target, return the indices i and j such that nums[i] + nums[j] == target and i != j.
You may assume that every input has exactly one pair of indices i and j that satisfy the condition.
Return the answer with the smaller index first.
Example 1:
Example 2:
# Hash Map {#hash-map}
def twoSum(nums, target):
num_to_index = {}
for i, num in enumerate(nums):
complement = target - num
if complement in num_to_index:
return [num_to_index[complement], i]
num_to_index[num] = i
# Brute Force {#brute-force}
def twoSum(nums, target):
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
# Hash Map and Sorting {#hash-map-and-sorting}
def groupAnagrams(strs):
anagrams = {}
for s in strs:
key = tuple(sorted(s))
if key in anagrams:
anagrams[key].append(s)
else:
anagrams[key] = [s]
return list(anagrams.values())
# Pythonic Solution {#pythonic-solution}
from collections import defaultdict
def groupAnagrams(strs):
anagrams = defaultdict(list)
for s in strs:
anagrams[tuple(sorted(s))].append(s)
return list(anagrams.values())
# Hash Map and Character Count {#hash-map-and-character-count}
def groupAnagrams(strs):
anagrams = {}
for s in strs:
count = [0] * 26
for char in s:
count[ord(char) - ord('a')] += 1
key = tuple(count)
if key in anagrams:
anagrams[key].append(s)
else:
anagrams[key] = [s]
return list(anagrams.values())
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
# Hash Map and Sorting {#hash-map-and-sorting}
def topKFrequent(nums, k):
freq = {}
for num in nums:
freq[num] = freq.get(num, 0) + 1
sorted_freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
return [x[0] for x in sorted_freq[:k]]
# Min Heap {#min-heap}
import heapq
def topKFrequent(nums, k):
freq = {}
for num in nums:
freq[num] = freq.get(num, 0) + 1
min_heap = []
for num, count in freq.items():
heapq.heappush(min_heap, (count, num))
if len(min_heap) > k:
heapq.heappop(min_heap)
return [num for count, num in min_heap]
# Bucket Sort {#bucket-sort}
from collections import Counter
def topKFrequent(nums, k):
freq = Counter(nums)
buckets = [[] for _ in range(len(nums) + 1)]
for num, count in freq.items():
buckets[count].append(num)
result = []
for i in range(len(buckets) - 1, -1, -1):
result.extend(buckets[i])
if len(result) >= k:
return result[:k]
Design an algorithm to encode a list of strings to a string and decode it back to the original list of strings.
# Length Prefix {#length-prefix}
def encode(strs):
encoded = ""
for s in strs:
encoded += str(len(s)) + "#" + s
return encoded
def decode(s):
decoded = []
i = 0
while i < len(s):
j = i
while s[j] != "#":
j += 1
length = int(s[i:j])
decoded.append(s[j + 1:j + 1 + length])
i = j + 1 + length
return decoded
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
Example 1:
# Prefix and Suffix Products {#prefix-and-suffix-products}
def productExceptSelf(nums):
n = len(nums)
prefix = [1] * n
suffix = [1] * n
for i in range(1, n):
prefix[i] = prefix[i - 1] * nums[i - 1]
suffix[n - i - 1] = suffix[n - i] * nums[n - i]
return [prefix[i] * suffix[i] for i in range(n)]
# Single Array (O(1) space) {#single-array-o1-space}
def productExceptSelf(nums):
n = len(nums)
result = [1] * n
prefix = 1
suffix = 1
for i in range(n):
result[i] *= prefix
prefix *= nums[i]
result[n - i - 1] *= suffix
suffix *= nums[n - i - 1]
return result