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
β”œβ”€β”€ πŸ“ 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!


πŸ”‘ 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