This repository contains π solutions to various data structure and algorithm problems from LeetCode. All solutions are implemented in Python and are structured for easy understanding and reusability.
A curated list of all LeetCode study plans
Automatic testcase generator for LeetCode problems
- π Each solution is stored in a separate Python file, named according to the problem number and title.
- π·οΈ Organized by category: Arrays, Strings, Trees, Graphs, and more.
π .
βββ π array
β βββ π easy
β β βββ 1.two_sum.py
β β βββ 1346.check_if_n_and_its_double_exist.py
β β βββ 1464.maximum_product_of_two_elements_in_an_array.py
β β βββ 1480.running_sum_of_1d_array.py
β β βββ 1491.average_salary_excluding_the_minimum_and_maximum_salary.py
β β βββ 1502.can_make_arithmetic_progression_from_sequence.py
β β βββ 1550.three_consecutive_odds.py
β β βββ 169.majority_element.py
β β βββ 1732.find_the_highest_altitude.py
β β βββ 1752.check_if_array_is_sorted_and_rotated.py
β β βββ 1800.maximum_ascending_subarray_sum.py
β β βββ 1920.build_array_from_permutation.py
β β βββ 2176.count_equal_and_divisible_pairs_in_an_array.py
β β βββ 2496.maximum_value_of_a_string_in_an_array.py
β β βββ 2873.maximum_value_of_an_ordered_triplet_i.py
β β βββ 2942.find_words_containing_character.py
β β βββ 3105.longest_strictly_increasing_or_strictly_decreasing_subarray.py
β β βββ 3151.special_array_i.py
β β βββ 3392.count_subarrays_of_length_three_with_a_condition.py
β β βββ 448.find_all_numbers_disappeared_in_an_array.py
β β βββ 48.rotate_image.py
β β βββ 66.plus_one.py
β β βββ 747.largest_number_at_least_twice_of_others.py
β β βββ 896.monotonic_array.py
β βββ π hard
β β βββ 41.first_missing_positive.py
β βββ π medium
β β βββ 189.rotate_array.py
β β βββ 2780.minimum_index_of_a_valid_split.py
β β βββ 2874.maximum_value_of_an_ordered_triplet_ii.py
β β βββ 57.insert_interval.py
β β βββ 915.partition_array_into_disjoint_intervals.py
βββ π backtracking
β βββ π medium
β β βββ 1980.find_unique_binary_string.py
βββ π binary-search
β βββ π easy
β β βββ 1385.find_the_distance_value_between_two_arrays.py
β β βββ 1539.kth_missing_positive_number.py
β β βββ 2529.maximum_count_of_positive_integer_and_negative_integer.py
β β βββ 2540.minimum_common_value.py
β β βββ 278.first_bad_version.py
β β βββ 35.search_insert_position.py
β β βββ 367.valid_perfect_square.py
β β βββ 374.guess_number_higher_or_lower.py
β β βββ 69.sqrtx.py
β β βββ 704.binary_search.py
β βββ π medium
β β βββ 153.find_minimum_in_rotated_sorted_array.py
β β βββ 1838.frequency_of_the_most_frequent_element.py
β β βββ 2226.maximum_candies_allocated_to_k_children.py
β β βββ 240.search_a_2d_matrix_ii.py
β β βββ 2513.minimize_the_maximum_of_two_arrays.py
β β βββ 2594.minimum_time_to_repair_cars.py
β β βββ 33.search_in_rotated_sorted_array.py
β β βββ 3356.zero_array_transformation_ii.py
β β βββ 34.find_first_and_last_position_of_element_in_sorted_array.py
β β βββ 74.search_a_2d_matrix.py
β β βββ 875.koko_eating_bananas.py
βββ π binary-tree
β βββ π easy
β β βββ 144.binary_tree_preorder_traversal.py
β β βββ 145.binary_tree_postorder_traversal.py
β β βββ 404.sum_of_left_leaves.py
β β βββ 94.binary_tree_inorder_traversal.py
βββ π bit-manipulation
β βββ π easy
β β βββ 1009.complement_of_base_10_integer.py
β β βββ 1018.binary_prefix_divisible_by_5.py
β β βββ 136.single_number.py
β β βββ 1486.xor_operation_in_an_array.py
β β βββ 191.number_of_1_bits.py
β β βββ 231.power_of_two.py
β β βββ 268.missing_number.py
β β βββ 3304.find_the_k_th_character_in_string_game_i.py
β β βββ 342.power_of_four.py
β β βββ 389.find_the_difference.py
β β βββ 476.number_complement.py
β βββ π medium
β β βββ 2401.longest_nice_subarray.py
β β βββ 2425.bitwise_xor_of_all_pairings.py
β β βββ 2429.minimize_xor.py
β β βββ 2683.neighboring_bitwise_xor.py
β β βββ 3191.minimum_operations_to_make_binary_array_elements_equal_to_one_i.py
βββ π breadth-first-search
β βββ π medium
β β βββ 102.binary_tree_level_order_traversal.py
β β βββ 103.binary_tree_zigzag_level_order_traversal.py
β β βββ 107.binary_tree_level_order_traversal_ii.py
β β βββ 1161.maximum_level_sum_of_a_binary_tree.py
β β βββ 1609.even_odd_tree.py
β β βββ 429.n_ary_tree_level_order_traversal.py
β β βββ 515.find_largest_value_in_each_tree_row.py
β β βββ 662.maximum_width_of_binary_tree.py
βββ π counting
β βββ π easy
β β βββ 1512.number_of_good_pairs.py
β β βββ 1790.check_if_one_string_swap_can_make_strings_equal.py
β β βββ 3442.maximum_difference_between_even_and_odd_frequency_i.py
β βββ π medium
β β βββ 2131.longest_palindrome_by_concatenating_two_letter_words.py
βββ π design
β βββ π hard
β β βββ 381.insert_delete_getrandom_o1_duplicates_allowed.py
β βββ π medium
β β βββ 380.insert_delete_getrandom_o1.py
βββ π divide-and-conquer
β βββ π easy
β β βββ 1763.longest_nice_substring.py
βββ π dynamic-programming
β βββ π easy
β β βββ 509.fibonacci_number.py
β βββ π medium
β β βββ 122.best_time_to_buy_and_sell_stock_ii.py
β β βββ 1749.maximum_absolute_sum_of_any_subarray.py
β β βββ 53.maximum_subarray.py
βββ π geometry
β βββ π easy
β β βββ 1232.check_if_it_is_a_straight_line.py
βββ π greedy-algorithm
β βββ π easy
β β βββ 2900.longest_unequal_adjacent_groups_subsequence_i.py
β β βββ 3074.apple_redistribution_into_boxes.py
β β βββ 680.valid_palindrome_ii.py
β βββ π medium
β β βββ 1007.minimum_domino_rotations_for_equal_row.py
β β βββ 1282.group_the_people_given_the_group_size_they_belong_to.py
β β βββ 2091.removing_minimum_and_maximum_from_array.py
β β βββ 2116.check_if_a_parentheses_string_can_be_valid.py
β β βββ 2918.minimum_equal_sum_of_two_arrays_after_replacing_zeros.py
β β βββ 452.minimum_number_of_arrows_to_burst_balloons.py
β β βββ 646.maximum_length_of_pair_chain.py
β β βββ 781.rabbits_in_forest.py
β β βββ 921.minimum_add_to_make_parentheses_valid.py
βββ π hash-table
β βββ π easy
β β βββ 1128.number_of_equivalent_domino_pairs.py
β β βββ 1207.unique_number_of_occurrences.py
β β βββ 1399.count_largest_group.py
β β βββ 1436.destination_city.py
β β βββ 2006.count_number_of_pairs_with_absolute_difference_k.py
β β βββ 205.isomorphic_strings.py
β β βββ 217.contains_duplicate.py
β β βββ 219.contains_duplicate_ii.py
β β βββ 2206.divide_array_into_equal_pairs.py
β β βββ 2404.most_frequent_even_element.py
β β βββ 3375.minimum_operations_to_make_array_values_equal_to_k.py
β β βββ 3396.minimum_number_of_operations_to_make_elements_in_array_distinct.py
β β βββ 383.ransom_note.py
β β βββ 575.distribute_candies.py
β βββ π medium
β β βββ 1400.construct_k_palindrome_strings.py
β β βββ 1726.tuple_with_same_product.py
β β βββ 1930.unique_length_3_palindromic_subsequences.py
β β βββ 2364.count_number_of_bad_pairs.py
β β βββ 2657.find_the_prefix_common_array_of_two_arrays.py
β β βββ 3159.find_occurrences_of_an_element_in_an_array.py
β β βββ 3160.find_the_number_of_distinct_colors_among_the_balls.py
β β βββ 3223.minimum_length_of_string_after_operations.py
β β βββ 49.group_anagrams.py
β β βββ 916.word_subsets.py
βββ π heap-priority-queue
β βββ π easy
β β βββ 2558.take_gifts_from_the_richest_pile.py
β β βββ 3065.minimum_operations_to_exceed_threshold_value_i.py
β βββ π hard
β β βββ 295.find_median_from_data_stream.py
β βββ π medium
β β βββ 2342.max_sum_of_a_pair_with_equal_sum_of_digits.py
β β βββ 2349.design_a_number_container_system.py
β β βββ 3066.minimum_operations_to_exceed_threshold_value_ii.py
β β βββ 451.sort_characters_by_frequency.py
βββ π linked-list
β βββ π easy
β β βββ 1290.convert_binary_number_in_a_linked_list_to_integer.py
β β βββ 141.linked_list_cycle.py
β β βββ 206.reverse_linked_list.py
β β βββ 234.palindrome_linked_list.py
β β βββ 83.remove_duplicates_from_sorted_list.py
β β βββ 876.middle_of_the_linked_list.py
β βββ π medium
β β βββ 142.linked_list_cycle_ii.py
β β βββ 143.reorder_list.py
β β βββ 2095.delete_the_middle_node_of_a_linked_list.py
β β βββ 61.rotate_list.py
β β βββ 82.remove_duplicates_from_sorted_list_ii.py
β β βββ 92.reverse_linked_list_ii.py
βββ π math
β βββ π easy
β β βββ 1217.minimum_cost_to_move_chips_to_the_same_position.py
β β βββ 1281.subtract_the_product_and_sum_of_digits_of_an_integer.py
β β βββ 1295.find_numbers_with_even_number_of_digits.py
β β βββ 1523.count_odd_numbers_in_an_interval_range.py
β β βββ 1588.sum_of_all_odd_length_subarrays.py
β β βββ 1822.sign_of_the_product_of_an_array.py
β β βββ 2520.count_the_digits_that_divide_a_number.py
β β βββ 2566.maximum_difference_by_remapping_a_digit.py
β β βββ 2843.count_symmetric_integers.py
β β βββ 2894.divisible_and_non_divisible_sums_difference.py
β β βββ 2965.find_missing_and_repeated_values.py
β β βββ 3024.type_of_triangle.py
β β βββ 326.power_of_three.py
β β βββ 9.palindrome_number.py
β βββ π medium
β β βββ 1780.check_if_number_is_a_sum_of_powers_of_three.py
β β βββ 2579.count_total_number_of_colored_cells.py
βββ π matrix
β βββ π easy
β β βββ 1572.matrix_diagonal_sum.py
β β βββ 1672.richest_customer_wealth.py
β β βββ 2133.check_if_every_row_and_column_contains_all_numbers.py
β β βββ 2643.row_with_maximum_ones.py
β β βββ 566.reshape_the_matrix.py
β βββ π medium
β β βββ 2661.first_completely_painted_row_or_column.py
β β βββ 73.set_matrix_zeroes.py
βββ π monotonic-stack
β βββ π easy
β β βββ 1475.final_prices_with_a_special_discount_in_a_shop.py
β β βββ 496.next_greater_element_i.py
β βββ π medium
β β βββ 2487.remove_nodes_from_linked_list.py
β β βββ 402.remove_k_digits.py
β β βββ 503.next_greater_element_ii.py
β β βββ 739.daily_temperatures.py
β β βββ 907.sum_of_subarray_minimums.py
βββ π prefix-sum
β βββ π easy
β β βββ 1854.maximum_population_year.py
β β βββ 2574.left_and_right_sum_differences.py
β β βββ 2848.points_that_intersect_with_cars.py
β β βββ 303.range_sum_query_immutable.py
β β βββ 3354.make_array_elements_equal_to_zero.py
β β βββ 3427.sum_of_variable_length_subarrays.py
β β βββ 3432.count_partitions_with_even_sum_difference.py
β βββ π medium
β β βββ 1352.product_of_the_last_k_numbers.py
β β βββ 1769.minimum_number_of_operations_to_move_all_balls_to_each_box.py
β β βββ 2121.intervals_between_identical_elements.py
β β βββ 2145.count_the_hidden_sequences.py
β β βββ 2270.number_of_ways_to_split_array.py
β β βββ 2381.shifting_letters_ii.py
β β βββ 2559.count_vowel_strings_in_ranges.py
β β βββ 2615.sum_of_distances.py
β β βββ 3355.zero_array_transformation_i.py
β β βββ 523.continuous_subarray_sum.py
β β βββ 525.contiguous_array.py
β β βββ 560.subarray_sum_equals_k.py
β β βββ 974.subarray_sums_divisible_by_k.py
βββ π sliding-window
β βββ π easy
β β βββ 2379.minimum_recolors_to_get_k_consecutive_black_blocks.py
β β βββ 3206.alternating_groups_i.py
β β βββ 643.maximum_average_subarray_i.py
β βββ π medium
β β βββ 1004.max_consecutive_ones_iii.py
β β βββ 1358.number_of_substrings_containing_all_three_characters.py
β β βββ 187.repeated_dna_sequences.py
β β βββ 209.minimum_size_subarray_sum.py
β β βββ 2461.maximum_sum_of_distinct_subarrays_with_length_k.py
β β βββ 2799.count_complete_subarrays_in_an_array.py
β β βββ 3.longest_substring_without_repeating_characters.py
β β βββ 3208.alternating_groups_ii.py
β β βββ 424.longest_repeating_character_replacement.py
β β βββ 438.find_all_anagrams_in_a_string.py
β β βββ 567.permutation_in_string.py
β β βββ 713.subarray_product_less_than_k.py
β β βββ 904.fruit_into_baskets.py
βββ π sorting
β βββ π easy
β β βββ 1636.sort_array_by_increasing_frequency.py
β β βββ 2099.find_subsequence_of_length_k_with_the_largest_sum.py
β β βββ 2164.sort_even_and_odd_indices_independently.py
β β βββ 3194.minimum_average_of_smallest_and_largest_elements.py
β βββ π medium
β β βββ 1887.reduction_operations_to_make_the_array_elements_equal.py
β β βββ 2033.minimum_operations_to_make_a_uni_value_grid.py
β β βββ 2294.partition_array_such_that_maximum_difference_is_k.py
β β βββ 2785.sort_vowels_in_a_string.py
β β βββ 2966.divide_array_into_arrays_with_max_difference.py
β β βββ 3085.minimum_deletions_to_make_string_k_special.py
β β βββ 3169.count_days_without_meetings.py
β β βββ 3394.check_if_grid_can_be_cut_into_sections.py
β β βββ 3443.maximum_manhattan_distance_after_k_changes.py
β β βββ 347.top_k_frequent_elements.py
β β βββ 442.find_all_duplicates_in_an_array.py
β β βββ 462.minimum_moves_to_equal_array_elements_ii.py
β β βββ 56.merge_intervals.py
βββ π stack
β βββ π easy
β β βββ 1047.remove_all_adjacent_duplicates_in_string.py
β β βββ 1544.make_the_string_great.py
β β βββ 20.valid_parentheses.py
β β βββ 3174.clear_digits.py
β βββ π medium
β β βββ 1081.smallest_subsequence_of_distinct_characters.py
β β βββ 1209.remove_all_adjacent_duplicates_in_string_ii.py
β β βββ 150.evaluate_reverse_polish_notation.py
β β βββ 155.min_stack.py
β β βββ 1910.remove_all_occurrences_of_a_substring.py
β β βββ 2390.removing_stars_from_a_string.py
β β βββ 316.remove_duplicate_letters.py
β β βββ 71.simplify_path.py
βββ π string
β βββ π easy
β β βββ 13.roman_to_integer.py
β β βββ 1309.decrypt_string_from_alphabet_to_integer_mapping.py
β β βββ 1422.maximum_score_after_splitting_a_string.py
β β βββ 1446.consecutive_characters.py
β β βββ 1678.goal_parser_interpretation.py
β β βββ 1832.check_if_the_sentence_is_pangram.py
β β βββ 1961.check_if_string_is_a_prefix_of_array.py
β β βββ 2124.check_if_all_as_appears_before_all_bs.py
β β βββ 2129.capitalize_the_title.py
β β βββ 2138.divide_a_string_into_groups_of_size_k.py
β β βββ 2255.count_prefixes_of_a_given_string.py
β β βββ 242.valid_anagram.py
β β βββ 2490.circular_sentence.py
β β βββ 3120.count_the_number_of_special_characters_i.py
β β βββ 387.first_unique_character_in_a_string.py
β β βββ 58.length_of_last_word.py
β β βββ 709.to_lower_case.py
β β βββ 953.verifying_an_alien_dictionary.py
β βββ π medium
β β βββ 2384.largest_palindromic_number.py
β β βββ 3121.count_the_number_of_special_characters_ii.py
β β βββ 38.count_and_say.py
β β βββ 8.string_to_integer_atoi.py
βββ π string-matching
β βββ π easy
β β βββ 1408.string_matching_in_an_array.py
β β βββ 28.find_the_index_of_the_first_occurrence_in_a_string.py
βββ π tree
β βββ π easy
β β βββ 589.n_ary_tree_preorder_traversal.py
β β βββ 590.n_ary_tree_postorder_traversal.py
βββ π trie
β βββ π easy
β β βββ 1455.check_if_a_word_occurs_as_a_prefix_of_any_word_in_a_sentence.py
β β βββ 2185.counting_words_with_a_given_prefix.py
β β βββ 3042.count_prefix_and_suffix_pairs_i.py
βββ π two-pointers
β βββ π easy
β β βββ 125.valid_palindrome.py
β β βββ 1768.merge_strings_alternately.py
β β βββ 202.happy_number.py
β β βββ 2200.find_all_k_distant_indices_in_an_array.py
β β βββ 2460.apply_operations_to_an_array.py
β β βββ 2570.merge_two_2d_arrays_by_summing_values.py
β β βββ 26.remove_duplicates_from_sorted_array.py
β β βββ 27.remove_element.py
β β βββ 2824.count_pairs_whose_sum_is_less_than_target.py
β β βββ 283.move_zeroes.py
β β βββ 344.reverse_string.py
β β βββ 345.reverse_vowels_of_a_string.py
β β βββ 3467.transform_array_by_parity.py
β β βββ 541.reverse_string_ii.py
β β βββ 844.backspace_string_compare.py
β β βββ 88.merge_sorted_array.py
β β βββ 905.sort_array_by_parity.py
β β βββ 922.sort_array_by_parity_ii.py
β β βββ 977.squares_of_a_sorted_array.py
β βββ π medium
β β βββ 11.container_with_most_water.py
β β βββ 15.3sum.py
β β βββ 16.3sum_closest.py
β β βββ 167.two_sum_ii_input_array_is_sorted.py
β β βββ 18.4sum.py
β β βββ 2161.partition_array_according_to_given_pivot.py
β β βββ 2563.count_the_number_of_fair_pairs.py
β β βββ 287.find_the_duplicate_number.py
β β βββ 457.circular_array_loop.py
β β βββ 581.shortest_unsorted_continuous_subarray.py
β β βββ 75.sort_colors.py
β β βββ 80.remove_duplicates_from_sorted_array_ii.py
β β βββ 986.interval_list_intersections.py
βββ LICENSE
βββ README.md
βββ TODO.md
βββ directory_layout_script.py
βββ lowercase_folder_names.py
βββ problem_description_script.py
βββ replace_dash_with_underscore_in_filenames.py
βββ requirements.txt
βββ safe_fix_leetcode_ids.py
π‘ Pro Tip: Click the filenames to view each solution directly on GitHub!
| π Category | π Description |
|---|---|
| π Arrays | Searching, sorting, subarray sums, and more. |
| π§΅ Strings | String manipulations, substrings, and pattern matching. |
| π Linked Lists | Singly, doubly linked list problems. |
| π² Trees | Binary trees, search trees, and n-ary trees. |
| πΊοΈ Graphs | Traversals, shortest paths, and connectivity. |
| π Dynamic Programming | Optimal substructure and overlapping subproblems. |
Each solution file contains:
- Problem Description: A detailed explanation of the problem, its constraints, and examples.
- Test Cases: Example inputs and expected outputs.
- Python Implementation: The function or class implementing the solution.
- Time and Space Complexity Analysis: A brief analysis of the efficiency of the solution.
Example:
"""
Problem Number: 1. Two Sum
Difficulty Level: Easy
https://leetcode.com/problems/two-sum/
********************************************************************************
Given an array of integers nums and an integer target, return indices of the two numbers
such that they add up to target. You may assume that each input would have exactly one
solution, and you may not use the same element twice. You can return the answer in any order
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
Constraints:
2 <= nums.length <= 10^4
-10^9 <= nums[i] <= 10^9
-10^9 <= target <= 10^9
Only one valid answer exists.
"""
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
pairs = dict()
for idx, num in enumerate(nums):
if target - num in pairs:
return [pairs[target - num], idx]
pairs[num] = idx
# Time Complexity: O(n)
# Space Complexity: O(n)