Skip to content

Commit b71615c

Browse files
committed
Added erlang
1 parent 5dea16b commit b71615c

File tree

70 files changed

+2491
-307
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2491
-307
lines changed

README.md

Lines changed: 307 additions & 307 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<source>src/main/dart</source>
9696
<source>src/main/c</source>
9797
<source>src/main/js</source>
98+
<source>src/main/erlang</source>
9899
</sources>
99100
</configuration>
100101
</execution>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
% #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
2+
% #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
3+
% #AI_can_be_used_to_solve_the_task #2025_01_05_Time_3_(97.50%)_Space_65.32_(7.50%)
4+
5+
-spec two_sum(Nums :: [integer()], Target :: integer()) -> [integer()].
6+
two_sum(Nums, Target) ->
7+
two_sum(Nums, Target, #{}, 0).
8+
9+
two_sum([], _, _, _) -> undefined;
10+
two_sum([H|T], Target, M, Index) ->
11+
case M of
12+
#{ Target-H := Pair } -> [Pair, Index];
13+
_ -> two_sum(T, Target, M#{ H => Index }, Index + 1)
14+
end.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1\. Two Sum
2+
3+
Easy
4+
5+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
6+
7+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
8+
9+
You can return the answer in any order.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,7,11,15], target = 9
14+
15+
**Output:** [0,1]
16+
17+
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,2,4], target = 6
22+
23+
**Output:** [1,2]
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [3,3], target = 6
28+
29+
**Output:** [0,1]
30+
31+
**Constraints:**
32+
33+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
34+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
35+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
36+
* **Only one valid answer exists.**
37+
38+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
2+
% #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
3+
% #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #AI_can_be_used_to_solve_the_task
4+
% #2025_01_05_Time_1_(77.78%)_Space_63.11_(100.00%)
5+
6+
%% Definition for singly-linked list.
7+
%%
8+
%% -record(list_node, {val = 0 :: integer(),
9+
%% next = null :: 'null' | #list_node{}}).
10+
11+
-spec add_two_numbers(L1 :: #list_node{} | null, L2 :: #list_node{} | null) -> #list_node{} | null.
12+
add_two_numbers(L1, L2) ->
13+
{Result, _} = add_two_numbers(L1, L2, 0),
14+
Result.
15+
16+
-spec add_two_numbers(L1 :: #list_node{} | null, L2 :: #list_node{} | null, Carry :: integer()) -> {#list_node{} | null, integer()}.
17+
add_two_numbers(null, null, 0) ->
18+
{null, 0};
19+
add_two_numbers(L1, L2, Carry) ->
20+
X = if L1 =/= null -> L1#list_node.val; true -> 0 end,
21+
Y = if L2 =/= null -> L2#list_node.val; true -> 0 end,
22+
Sum = Carry + X + Y,
23+
NewCarry = Sum div 10,
24+
NewNode = #list_node{val = Sum rem 10},
25+
{Next1, _} = if L1 =/= null -> {L1#list_node.next, 0}; true -> {null, 0} end,
26+
{Next2, _} = if L2 =/= null -> {L2#list_node.next, 0}; true -> {null, 0} end,
27+
{NextResult, _} = add_two_numbers(Next1, Next2, NewCarry),
28+
{NewNode#list_node{next = NextResult}, NewCarry}.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
14+
15+
**Output:** [7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 = [0], l2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
28+
29+
**Output:** [8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range `[1, 100]`.
34+
* `0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
2+
% #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
3+
% #Big_O_Time_O(n)_Space_O(1) #AI_can_be_used_to_solve_the_task
4+
% #2025_01_08_Time_11_(100.00%)_Space_61.60_(60.00%)
5+
6+
-spec length_of_longest_substring(S :: unicode:unicode_binary()) -> integer().
7+
length_of_longest_substring(S) ->
8+
do(S, 0, #{}, 0, 1).
9+
10+
do(<<Char, Rest/binary>>, Index, PrevPos, Max, Acc0) when is_map_key(Char, PrevPos) ->
11+
PrevIndex = map_get(Char, PrevPos),
12+
Acc1 = Index - PrevIndex,
13+
Acc = min(Acc1, Acc0),
14+
do(Rest, Index + 1, PrevPos#{Char => Index}, max(Max, Acc), Acc + 1);
15+
do(<<Char, Rest/binary>>, Index, PrevPos, Max, Acc) when Acc > Max ->
16+
do(Rest, Index + 1, PrevPos#{Char => Index}, Acc, Acc + 1);
17+
do(<<Char, Rest/binary>>, Index, PrevPos, Max, Acc) ->
18+
do(Rest, Index + 1, PrevPos#{Char => Index}, Max, Acc + 1);
19+
20+
do(<<>>, _, _, Max, _) -> Max.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3\. Longest Substring Without Repeating Characters
2+
3+
Medium
4+
5+
Given a string `s`, find the length of the **longest substring** without repeating characters.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abcabcbb"
10+
11+
**Output:** 3
12+
13+
**Explanation:** The answer is "abc", with the length of 3.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "bbbbb"
18+
19+
**Output:** 1
20+
21+
**Explanation:** The answer is "b", with the length of 1.
22+
23+
**Example 3:**
24+
25+
**Input:** s = "pwwkew"
26+
27+
**Output:** 3
28+
29+
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
30+
31+
**Example 4:**
32+
33+
**Input:** s = ""
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
40+
* `s` consists of English letters, digits, symbols and spaces.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
% #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
2+
% #Big_O_Time_O(log(min(N,M)))_Space_O(1) #AI_can_be_used_to_solve_the_task
3+
% #2025_01_08_Time_1_(100.00%)_Space_65.96_(100.00%)
4+
5+
-spec find_median_sorted_arrays(Nums1 :: [integer()], Nums2 :: [integer()]) -> float().
6+
find_median_sorted_arrays(Nums1, Nums2) ->
7+
Len = length(Nums1) + length(Nums2),
8+
find_median_sorted_arrays(Nums1, Nums2, [], -1, 0, 0, Len rem 2, Len div 2).
9+
find_median_sorted_arrays(_, _, [H|T], Index, _, _, Even, Index) ->
10+
if Even =:= 1 -> H;
11+
true -> (H + lists:nth(1, T)) / 2
12+
end;
13+
find_median_sorted_arrays([], [H2|T2], MyArr, Index, Indx1, Indx2, Even, Middle) ->
14+
find_median_sorted_arrays([], T2, [H2|MyArr], Index + 1, Indx1, Indx2 + 1, Even, Middle);
15+
find_median_sorted_arrays([H1|T1], [], MyArr, Index, Indx1, Indx2, Even, Middle) ->
16+
find_median_sorted_arrays(T1, [], [H1|MyArr], Index + 1, Indx1, Indx2 + 1, Even, Middle);
17+
find_median_sorted_arrays([H1|T1], [H2|T2], MyArr, Index, Indx1, Indx2, Even, Middle) when H1 < H2 ->
18+
find_median_sorted_arrays(T1, [H2|T2], [H1|MyArr], Index + 1, Indx1 + 1, Indx2, Even, Middle);
19+
find_median_sorted_arrays([H1|T1], [H2|T2], MyArr, Index, Indx1, Indx2, Even, Middle)->
20+
find_median_sorted_arrays([H1|T1], T2, [H2|MyArr], Index + 1, Indx1, Indx2 + 1, Even, Middle).
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
4\. Median of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
6+
7+
The overall run time complexity should be `O(log (m+n))`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums1 = [1,3], nums2 = [2]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** merged array = [1,2,3] and median is 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums1 = [1,2], nums2 = [3,4]
20+
21+
**Output:** 2.50000
22+
23+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
24+
25+
**Example 3:**
26+
27+
**Input:** nums1 = [0,0], nums2 = [0,0]
28+
29+
**Output:** 0.00000
30+
31+
**Example 4:**
32+
33+
**Input:** nums1 = [], nums2 = [1]
34+
35+
**Output:** 1.00000
36+
37+
**Example 5:**
38+
39+
**Input:** nums1 = [2], nums2 = []
40+
41+
**Output:** 2.00000
42+
43+
**Constraints:**
44+
45+
* `nums1.length == m`
46+
* `nums2.length == n`
47+
* `0 <= m <= 1000`
48+
* `0 <= n <= 1000`
49+
* `1 <= m + n <= 2000`
50+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
2+
% #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
3+
% #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
4+
% #2025_01_08_Time_179_(100.00%)_Space_59.84_(100.00%)
5+
6+
-spec longest_palindrome(S :: unicode:unicode_binary()) -> unicode:unicode_binary().
7+
longest_palindrome(S) ->
8+
Length = byte_size(S),
9+
case Length of
10+
0 -> <<>>; % Return empty binary if input is empty
11+
_ ->
12+
% Initialize variables for the best palindrome
13+
{Start, Len} = lists:foldl(
14+
fun(Index, {BestStart, BestLen}) ->
15+
% Find the longest palindrome for odd and even length centers
16+
{NewStart1, NewLen1} = find_longest_palindrome(S, Index, Index),
17+
{NewStart2, NewLen2} = find_longest_palindrome(S, Index, Index + 1),
18+
% Choose the longer of the two found palindromes
19+
case NewLen1 >= NewLen2 of
20+
true -> update_best(BestStart, BestLen, NewStart1, NewLen1);
21+
false -> update_best(BestStart, BestLen, NewStart2, NewLen2)
22+
end
23+
end,
24+
{0, 0}, % Initial best palindrome start and length
25+
lists:seq(0, Length - 1) % Iterate through all positions
26+
),
27+
% Extract the longest palindromic substring
28+
binary:part(S, Start, Len)
29+
end.
30+
31+
% Helper function to find the longest palindrome around the center
32+
-spec find_longest_palindrome(S :: unicode:unicode_binary(), L :: integer(), R :: integer()) -> {integer(), integer()}.
33+
find_longest_palindrome(S, L, R) ->
34+
Length = byte_size(S),
35+
case (L >= 0 andalso R < Length andalso binary:part(S, L, 1) =:= binary:part(S, R, 1)) of
36+
true ->
37+
find_longest_palindrome(S, L - 1, R + 1);
38+
false ->
39+
{L + 1, R - L - 1}
40+
end.
41+
42+
% Helper function to update the best palindrome found so far
43+
-spec update_best(BestStart :: integer(), BestLen :: integer(), NewStart :: integer(), NewLen :: integer()) -> {integer(), integer()}.
44+
update_best(BestStart, BestLen, NewStart, NewLen) ->
45+
case NewLen > BestLen of
46+
true -> {NewStart, NewLen};
47+
false -> {BestStart, BestLen}
48+
end.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
5\. Longest Palindromic Substring
2+
3+
Medium
4+
5+
Given a string `s`, return _the longest palindromic substring_ in `s`.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "babad"
10+
11+
**Output:** "bab" **Note:** "aba" is also a valid answer.
12+
13+
**Example 2:**
14+
15+
**Input:** s = "cbbd"
16+
17+
**Output:** "bb"
18+
19+
**Example 3:**
20+
21+
**Input:** s = "a"
22+
23+
**Output:** "a"
24+
25+
**Example 4:**
26+
27+
**Input:** s = "ac"
28+
29+
**Output:** "a"
30+
31+
**Constraints:**
32+
33+
* `1 <= s.length <= 1000`
34+
* `s` consist of only digits and English letters.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
% #Medium #String #2025_01_08_Time_203_(100.00%)_Space_60.52_(100.00%)
2+
3+
%% Define the function specification
4+
-spec convert(S :: unicode:unicode_binary(), NumRows :: integer()) -> unicode:unicode_binary().
5+
convert(S, NumRows) ->
6+
%% Convert the input string to a list of characters
7+
CharList = unicode:characters_to_list(S),
8+
Length = length(CharList),
9+
%% Handle edge cases
10+
if
11+
NumRows == 1; NumRows >= Length ->
12+
S;
13+
true ->
14+
%% Initialize a list of empty lists (rows) with length NumRows
15+
Rows = lists:map(fun(_) -> [] end, lists:seq(1, NumRows)),
16+
%% Build the zigzag pattern
17+
ZigzagRows = build_zigzag(CharList, Rows, 0, true),
18+
%% Concatenate all rows to form the result
19+
unicode:characters_to_binary(lists:append(ZigzagRows))
20+
end.
21+
22+
%% Recursive function to build zigzag rows
23+
-spec build_zigzag([integer()], [[integer()]], integer(), boolean()) -> [[integer()]].
24+
build_zigzag([], Rows, _, _) ->
25+
Rows;
26+
build_zigzag([Char | Rest], Rows, CurrentRow, GoingDown) ->
27+
%% Add the character to the current row
28+
UpdatedRows = update_row(Rows, CurrentRow, Char),
29+
%% Determine the new direction and row index
30+
{NewDirection, NewRow} = case {CurrentRow, GoingDown} of
31+
{0, _} -> {true, 1};
32+
{Row, _} when Row == length(Rows) - 1 -> {false, Row - 1};
33+
{Row, true} -> {true, Row + 1};
34+
{Row, false} -> {false, Row - 1}
35+
end,
36+
%% Recursively build the zigzag pattern
37+
build_zigzag(Rest, UpdatedRows, NewRow, NewDirection).
38+
39+
%% Helper function to update the row with the new character
40+
-spec update_row([[integer()]], integer(), integer()) -> [[integer()]].
41+
update_row(Rows, Index, Char) ->
42+
lists:sublist(Rows, Index) ++ [lists:nth(Index + 1, Rows) ++ [Char]] ++ lists:nthtail(Index + 1, Rows).

0 commit comments

Comments
 (0)