Problems
Build your DSA foundation one pattern at a time.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Given a string s containing only the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: 1. Open brackets are closed by the same type of bracket. 2. Open brackets are closed in the correct order. 3. Every closing bracket has a corresponding open bracket.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
Practice this problem and implement an efficient solution.
TODO: Add the complete problem description.
TODO: Add the complete problem description.
TODO: Add the complete problem description.
TODO: Add the complete problem description.
You are given an integer array nums sorted in ascending order with distinct values. Before being passed to your function, nums may have been rotated at an unknown pivot. Given nums and target, return the index of target if it exists. Otherwise, return -1. Your solution should run in O(log n) time.
A peak element is an element that is strictly greater than its neighbors. Given a 0-indexed integer array nums, return the index of any peak element. You may imagine nums[-1] = nums[n] = -infinity.
Given an array of integers nums and an integer target, return the indices of the two numbers such that they add up to target. You may assume that each input has exactly one solution, and you may not use the same element twice. You can return the answer in any order.
Given an integer array nums, return true if any value appears at least twice in the array. Return false if every element is distinct.
You are given an array prices where prices[i] represents the price of a stock on the ith day. You want to maximize your profit by choosing one day to buy one stock and choosing a later day to sell that stock. Return the maximum profit you can achieve. If no profit can be achieved, return 0.
Given an integer array nums, find the contiguous subarray with the largest sum and return its sum. The subarray must contain at least one element.
Given an integer array nums, return an array answer where answer[i] equals the product of every element of nums except nums[i]. Solve it without using division.
Given an array nums of size n, return the majority element. The majority element appears more than floor(n / 2) times and is guaranteed to exist.
Given an integer array nums, move all zeroes to the end while maintaining the relative order of all non-zero elements. Return the transformed array.
Given an integer array nums and a non-negative integer k, rotate the array to the right by k positions and return the resulting array.
Given an array nums containing n distinct numbers taken from the range [0, n], return the only number from that range that is missing.
Given an integer array nums, find the contiguous non-empty subarray that has the largest product and return that product.
Given an integer array nums, return the leftmost pivot index where the sum of elements strictly to the left equals the sum of elements strictly to the right. Return -1 if no pivot exists.
Given an array nums containing only 0, 1 and 2, sort the values in-place so equal values are adjacent and ordered 0, 1, 2. Return the sorted array.
Given an integer array nums representing a permutation, rearrange it into the lexicographically next greater permutation. If no greater permutation exists, rearrange it into the smallest possible order. Return the resulting array.
Given two sorted integer arrays nums1 and nums2, return a single sorted array containing all elements from both arrays.
Given a sorted integer array nums, return an array containing each distinct value exactly once while preserving sorted order.
Given an unsorted integer array nums, return the smallest positive integer that does not appear in nums.
Given an integer array nums of length n where every value is between 1 and n, return all numbers in the range [1, n] that do not appear in nums.
Given an integer array nums of length n where each value is between 1 and n and each value appears once or twice, return all values that appear twice.
Given an m x n integer matrix, if an element is 0, set its entire row and column to 0. Return the resulting matrix.
Given an m x n matrix, return all elements of the matrix in spiral order.
Given an n x n matrix, rotate the matrix 90 degrees clockwise and return the rotated matrix.
You are given an integer array nums where nums[i] represents the maximum jump length from index i. Return true if you can reach the final index starting from index 0.
There are n gas stations arranged in a circle. gas[i] is the fuel available at station i and cost[i] is the fuel required to travel from station i to station i + 1. Return the starting station index from which the complete circuit can be traveled clockwise, or -1 if impossible.
There are n children standing in a line, each with a rating. Every child must receive at least one candy, and children with a higher rating than an adjacent child must receive more candies than that child. Return the minimum total number of candies required.
Given an integer array height where height[i] represents the height of a vertical line at position i, choose two lines that together with the x-axis form a container. Return the maximum amount of water the container can hold.
Given n non-negative integers representing an elevation map where the width of each bar is 1, return how much rain water can be trapped after raining.
Given a circular integer array nums, return the maximum possible sum of a non-empty subarray. A circular subarray may wrap from the end of the array back to the beginning, but each array element may be used at most once.
Given an integer array nums and an integer k, return the total number of contiguous non-empty subarrays whose sum equals k.
Given an integer array nums and an integer val, remove every occurrence of val conceptually in-place and return the remaining values while preserving their original relative order.
Given an array nums containing n + 1 integers where every integer is in the range 1 to n inclusive, return the single repeated number. The repeated value may occur more than twice.
Given an integer array nums, return true if there exist indices i < j < k such that nums[i] < nums[j] < nums[k]. Otherwise return false.
Given an array citations where citations[i] is the number of citations received by a researcher's ith paper, return the researcher's h-index. The h-index is the maximum h such that at least h papers each have at least h citations.
Given an integer numRows, return the first numRows of Pascal's triangle. Each interior value is the sum of the two values directly above it.
Given a zero-based rowIndex, return the rowIndex-th row of Pascal's triangle.
Given an m x n binary board representing cells in Conway's Game of Life, compute and return the board after one simultaneous update. A live cell survives with two or three live neighbors, while a dead cell becomes live with exactly three live neighbors.
You are given multiple integer arrays, each sorted in non-decreasing order. Choose one integer from one array and another integer from a different array. Return the maximum possible absolute difference between the two chosen integers.
Given two strings s and t, return true if t is an anagram of s. An anagram is formed by rearranging the letters of another word using all original characters exactly once.
Given an array of strings strs, group the anagrams together. Strings belong to the same group when they contain exactly the same characters with the same frequencies. The groups and the strings inside each group may be returned in any order.
Given an integer array nums and an integer k, return the k most frequent distinct elements. The answer may be returned in any order.
Given a partially filled 9 by 9 Sudoku board, determine whether the current board is valid. Each row, each column and each 3 by 3 sub-box must contain each digit from 1 through 9 at most once. Empty cells are represented by a period.
Given two strings s and t, determine whether they are isomorphic. Every occurrence of a character in s must map to the same character in t, and two different characters in s cannot map to the same character in t.
Given a pattern and a space-separated string s, determine whether s follows the same pattern. Each pattern character must map to exactly one word and each word must map to exactly one pattern character.
Starting with a positive integer n, repeatedly replace the number by the sum of the squares of its digits. Return true if the process eventually reaches 1. Return false if it enters a cycle that never reaches 1.
Given two integer arrays nums1 and nums2, return an array containing their distinct common elements. Each value must appear only once in the result, and the result may be returned in any order.
Given two integer arrays nums1 and nums2, return their intersection while preserving multiplicity. Each element in the result must appear as many times as it occurs in both arrays. The result may be returned in any order.
Given two strings ransomNote and magazine, return true if ransomNote can be constructed using characters from magazine. Each character in magazine may be used at most once.
Given a string s, find the first character that appears exactly once and return its index. Return -1 if no unique character exists.
Given a string s containing lowercase and uppercase English letters, return the length of the longest palindrome that can be built using those characters. Character matching is case-sensitive.
Given a string s, sort its characters in decreasing order based on their frequency and return the resulting string. Characters with the same frequency may appear in any relative order, but identical characters must remain grouped together.
Given an array of strings words, return all characters that appear in every string, including duplicates. The result may be returned in any order.
Given an array of integers arr, return true if the number of occurrences of every distinct value is unique. Otherwise return false.
Two strings are considered close if one can be transformed into the other using any number of swaps between existing characters and swaps of the identities of two existing characters. Return true if word1 and word2 are close.
Given an n by n integer matrix grid, return the number of pairs consisting of a row and a column that contain exactly the same values in exactly the same order.
A rectangular wall consists of rows of bricks. Every row has the same total width, but brick widths may differ. Draw one vertical line from top to bottom so that it crosses the fewest bricks. The line may pass through brick boundaries but cannot be drawn along either outer edge. Return the minimum number of crossed bricks.
A harmonious subsequence is a subsequence in which the difference between its maximum and minimum values is exactly 1. Given an integer array nums, return the length of its longest harmonious subsequence.
You are given two equal-length digit strings secret and guess. A bull is a digit that matches in both value and position. A cow is a non-bull digit from guess that exists elsewhere in secret. Return the hint in the form xAyB, where x is the number of bulls and y is the number of cows.
You are given directory descriptions. Each description contains a directory path followed by one or more files represented as fileName(content). Return groups of complete file paths whose files have identical content. Only groups containing at least two files should be returned. Groups and paths within groups may be returned in any order.
Given two arrays of unique strings list1 and list2, find all common strings whose index sum is the smallest among all common strings. Return the answer in any order.
Given an array of English words, return the words that can be typed using letters from only one row of a standard American keyboard. Letter matching is case-insensitive. The result may be returned in any order.
A word is uncommon if it appears exactly once in one sentence and does not appear in the other sentence. Given two space-separated sentences s1 and s2, return all uncommon words in any order.
Given an unsorted array of integers nums, return the length of the longest sequence of consecutive integer values. The values do not need to appear consecutively in the original array.
A count-paired domain contains a visit count followed by a domain. A visit to a domain also counts as a visit to every parent subdomain. Given an array cpdomains, return the accumulated visit count for every visited domain and subdomain. The result may be returned in any order.
You are given two strings s and t. String t is created by randomly shuffling string s and then adding one extra character. Return the character that was added.
A pangram is a sentence in which every lowercase English letter appears at least once. Given a string sentence containing lowercase English letters, return true if sentence is a pangram and false otherwise.
You are given strings jewels and stones. Every character in stones represents a stone you have, while every character in jewels represents a type of stone considered a jewel. Letters are case-sensitive. Return how many stones are jewels.
Given an integer array nums and an integer k, return true if there are two distinct indices i and j such that nums[i] equals nums[j] and the absolute difference between i and j is at most k.
The degree of an array is the maximum frequency of any of its elements. Given a non-empty integer array nums, return the smallest possible length of a contiguous subarray that has the same degree as nums.
Given an array of integers nums, return the number of good pairs. A pair of indices (i, j) is good when nums[i] equals nums[j] and i is less than j.
Given an integer array nums and a non-negative integer k, return the number of unique pairs of values whose absolute difference is exactly k. Pairs with the same two values count only once.
An original array was transformed by appending twice every element and then shuffling the result. Given the resulting non-negative integer array changed, reconstruct and return one valid original array. If no original array exists, return an empty array. The returned original values may appear in any order.
Given an integer array arr of even length, return true if it can be reordered so that for every pair of adjacent conceptual elements, one value is exactly twice the other. Equivalently, every value must be matched with its double.
You are given an array time where time[i] is the duration of a song in seconds. Return the number of index pairs (i, j), with i less than j, for which time[i] + time[j] is divisible by 60.