Logo
MainBooksThoughtsAbout

Patterns

Elixir solutions

LeetCode 3900 problem - Prefix Sum pattern, O(n) complexity solution

LeetCode 3900. Longest Balanced Substring After One Swap combines the Prefix Sum pattern with a Hash Map technique to find the longest balanced substring after at most one swap. By tracking the difference between zeros and ones, we can determine whether a substring is already balanced or can be balanced with a single swap. The solution runs in O(n) time using a single pass through the string, with O(n) space for storing prefix balance information.

Read more

LeetCode 678 solution using the Greedy pattern

LeetCode 678. Valid Parenthesis String can be solved using the Greedy pattern by tracking a range of possible open parentheses. low represents the minimum possible number of unmatched '(', while high represents the maximum. For '*', the range expands because it can act as '(', ')', or an empty string. If high becomes negative, there are too many closing parentheses, so the string is invalid. At the end, the string is valid if low == 0. This approach runs in O(n) time and uses O(1) space complexity.

Read more

LeetCode 32 Longest Valid Parentheses problem solution

LeetCode 32. Longest Valid Parentheses can be solved using a Stack. Store indices of unmatched parentheses and use -1 as the initial boundary. When a matching pair is found, calculate the current valid substring length from the index on top of the stack. If the stack becomes empty, the current index becomes the new boundary. The solution runs in O(n) time and uses O(n) space complexity.

Read more

22 LeetCode Generate Parentheses problem solution

LeetCode 22. Generate Parentheses uses the Backtracking technique to generate only valid combinations. The number of results follows the Catalan number Cₙ. Time complexity is O(n * Cₙ), with O(n) auxiliary space or O(n * Cₙ) including the output.

Read more

LeetCode 22. Generate Parentheses Elixir solution

The Elixir solution uses Backtracking with recursion to generate only valid parentheses combinations. The open and close counters track how many opening and closing parentheses have been used. The Time complexity is O(n * Cₙ) and the auxiliary Space complexity is O(n).

Read more
PreviousPage 2 of 13Next
☕Support AlgoBytes on Patreon

© 2026 Algobytes

Privaci Policy