15 Jun 2024ElixirUnknown

Removing Stars From A String

notes and solution files for removing stars from a string.

this entry collects the solution files i have for removing stars from a string. i may expand it with a fuller write-up later, but the implementation files are already here.

available solution files

  • Elixir removing-stars-from-a-string/removing-stars-from-a-string.ex

Solution files

Elixirremoving-stars-from-a-string/removing-stars-from-a-string.ex
defmodule Solution do
  @spec remove_stars(s :: String.t) :: String.t
  def remove_stars(s) do
    s
    |> String.split("", trim: true)
    |> Enum.reverse()
    |> compute(0, [])
    |> Enum.join("")
  end

  defp compute([], _, ret), do: ret
  defp compute(["*" | s], stars, ret), do: compute(s, stars+1, ret)
  defp compute([c | s], 0, ret), do: compute(s, 0, [c | ret])
  defp compute([c | s], stars, ret), do: compute(s, stars-1, ret)
end