Recursive solution for Longest Substring Without Repeating Characters written in Elixir

The name of the pictureThe name of the pictureThe name of the pictureClash 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






share|improve this question

























    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






    share|improve this question





















      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






      share|improve this question











      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








      share|improve this question










      share|improve this question




      share|improve this question









      asked Apr 3 at 11:24









      Eli

      212




      212

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?