Skip to content

abdulaziz7225/leetcode-problems

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Solving Data Structure and Algorithm Problems on LeetCode Using Python

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

LeetCode Monthly Challenge

🗂️ Repository Structure

  • 📂 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.

Directory Layout


📂 .
├── 📁 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
├── 📁 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
│   │   ├── 503.next_greater_element_ii.py
│   │   └── 739.daily_temperatures.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
│   │   ├── 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!


🔑 Problem Categories

📂 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.

🛠️ Solution Format

Each solution file contains:

  1. Problem Description: A detailed explanation of the problem, its constraints, and examples.
  2. Test Cases: Example inputs and expected outputs.
  3. Python Implementation: The function or class implementing the solution.
  4. 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)

About

Solving Data Structure and Algorithm Problems on LeetCode Using Python

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages