Recursive solution for Longest Substring Without Repeating Characters written in Elixir
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
4
down vote
favorite
Here's my recursive implementation of a sliding window algorithm to find the longest substring of a given string without repeating characters. Is there a more idiomatic-Elixir way to write this?
defmodule MyString do
def length_of_longest_substring(string), do: string |> length_of_longest_substring(0, 0, 0, %)
defp length_of_longest_substring(string, start_index, end_index, longest, indexes) do
if end_index >= String.length(string) do
longest
else
currrent_char = String.at(string, end_index)
previous_occurrence = Map.get(indexes, currrent_char, -1)
updated_indexes = Map.put(indexes, currrent_char, end_index)
cond do
previous_occurrence >= start_index ->
string |> length_of_longest_substring(previous_occurrence + 1, end_index, longest, updated_indexes)
true ->
longest = max(longest, end_index - start_index + 1)
string |> length_of_longest_substring(start_index, end_index + 1, longest, updated_indexes)
end
end
end
end
algorithm recursion functional-programming elixir
add a comment |Â
up vote
4
down vote
favorite
Here's my recursive implementation of a sliding window algorithm to find the longest substring of a given string without repeating characters. Is there a more idiomatic-Elixir way to write this?
defmodule MyString do
def length_of_longest_substring(string), do: string |> length_of_longest_substring(0, 0, 0, %)
defp length_of_longest_substring(string, start_index, end_index, longest, indexes) do
if end_index >= String.length(string) do
longest
else
currrent_char = String.at(string, end_index)
previous_occurrence = Map.get(indexes, currrent_char, -1)
updated_indexes = Map.put(indexes, currrent_char, end_index)
cond do
previous_occurrence >= start_index ->
string |> length_of_longest_substring(previous_occurrence + 1, end_index, longest, updated_indexes)
true ->
longest = max(longest, end_index - start_index + 1)
string |> length_of_longest_substring(start_index, end_index + 1, longest, updated_indexes)
end
end
end
end
algorithm recursion functional-programming elixir
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
Here's my recursive implementation of a sliding window algorithm to find the longest substring of a given string without repeating characters. Is there a more idiomatic-Elixir way to write this?
defmodule MyString do
def length_of_longest_substring(string), do: string |> length_of_longest_substring(0, 0, 0, %)
defp length_of_longest_substring(string, start_index, end_index, longest, indexes) do
if end_index >= String.length(string) do
longest
else
currrent_char = String.at(string, end_index)
previous_occurrence = Map.get(indexes, currrent_char, -1)
updated_indexes = Map.put(indexes, currrent_char, end_index)
cond do
previous_occurrence >= start_index ->
string |> length_of_longest_substring(previous_occurrence + 1, end_index, longest, updated_indexes)
true ->
longest = max(longest, end_index - start_index + 1)
string |> length_of_longest_substring(start_index, end_index + 1, longest, updated_indexes)
end
end
end
end
algorithm recursion functional-programming elixir
Here's my recursive implementation of a sliding window algorithm to find the longest substring of a given string without repeating characters. Is there a more idiomatic-Elixir way to write this?
defmodule MyString do
def length_of_longest_substring(string), do: string |> length_of_longest_substring(0, 0, 0, %)
defp length_of_longest_substring(string, start_index, end_index, longest, indexes) do
if end_index >= String.length(string) do
longest
else
currrent_char = String.at(string, end_index)
previous_occurrence = Map.get(indexes, currrent_char, -1)
updated_indexes = Map.put(indexes, currrent_char, end_index)
cond do
previous_occurrence >= start_index ->
string |> length_of_longest_substring(previous_occurrence + 1, end_index, longest, updated_indexes)
true ->
longest = max(longest, end_index - start_index + 1)
string |> length_of_longest_substring(start_index, end_index + 1, longest, updated_indexes)
end
end
end
end
algorithm recursion functional-programming elixir
asked Apr 3 at 11:24
Eli
212
212
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191149%2frecursive-solution-for-longest-substring-without-repeating-characters-written-in%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password